mirror of
https://github.com/fosrl/newt.git
synced 2026-03-04 09:46:44 +00:00
Fix nil issues
This commit is contained in:
15
linux.go
15
linux.go
@@ -60,20 +60,23 @@ func closeClients() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func clientsHandleNewtConnection(publicKey string) {
|
func clientsHandleNewtConnection(publicKey string) {
|
||||||
if wgService != nil {
|
if wgService == nil {
|
||||||
wgService.SetServerPubKey(publicKey)
|
return
|
||||||
} else {
|
|
||||||
logger.Error("WireGuard service is not initialized, cannot set server public key")
|
|
||||||
}
|
}
|
||||||
|
wgService.SetServerPubKey(publicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clientsOnConnect() {
|
func clientsOnConnect() {
|
||||||
if wgService != nil {
|
if wgService == nil {
|
||||||
wgService.LoadRemoteConfig()
|
return
|
||||||
}
|
}
|
||||||
|
wgService.LoadRemoteConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
func clientsAddProxyTarget(pm *proxy.ProxyManager, tunnelIp string) {
|
func clientsAddProxyTarget(pm *proxy.ProxyManager, tunnelIp string) {
|
||||||
|
if wgService == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
// add a udp proxy for localost and the wgService port
|
// add a udp proxy for localost and the wgService port
|
||||||
// TODO: make sure this port is not used in a target
|
// TODO: make sure this port is not used in a target
|
||||||
pm.AddTarget("udp", tunnelIp, int(wgService.Port), fmt.Sprintf("127.0.0.1:%d", wgService.Port))
|
pm.AddTarget("udp", tunnelIp, int(wgService.Port), fmt.Sprintf("127.0.0.1:%d", wgService.Port))
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -547,7 +547,8 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
"pingResults": pingResults,
|
"pingResults": pingResults,
|
||||||
"newtVersion": newtVersion,
|
"newtVersion": newtVersion,
|
||||||
}, 1*time.Second)
|
}, 1*time.Second)
|
||||||
logger.Info("Sent exit node ping results to cloud for selection")
|
|
||||||
|
logger.Debug("Sent exit node ping results to cloud for selection: pingResults=%+v", pingResults)
|
||||||
})
|
})
|
||||||
|
|
||||||
client.RegisterHandler("newt/tcp/add", func(msg websocket.WSMessage) {
|
client.RegisterHandler("newt/tcp/add", func(msg websocket.WSMessage) {
|
||||||
@@ -684,7 +685,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to send registration message: %v", err)
|
logger.Error("Failed to send registration message: %v", err)
|
||||||
}
|
}
|
||||||
logger.Info("Sent registration message")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to send Docker container list: %v", err)
|
logger.Error("Failed to send Docker container list: %v", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -714,6 +715,7 @@ persistent_keepalive_interval=5`, fixKey(privateKey.String()), fixKey(wgData.Pub
|
|||||||
logger.Error("Failed to send registration message: %v", err)
|
logger.Error("Failed to send registration message: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("Sent registration message")
|
logger.Info("Sent registration message")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -126,6 +126,8 @@ func (c *Client) SendMessage(messageType string, data interface{}) error {
|
|||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Debug("Sending message: %s, data: %+v", messageType, data)
|
||||||
|
|
||||||
c.writeMux.Lock()
|
c.writeMux.Lock()
|
||||||
defer c.writeMux.Unlock()
|
defer c.writeMux.Unlock()
|
||||||
return c.conn.WriteJSON(msg)
|
return c.conn.WriteJSON(msg)
|
||||||
@@ -182,62 +184,6 @@ func (c *Client) getToken() (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we already have a token, try to use it
|
|
||||||
if c.config.Token != "" {
|
|
||||||
tokenCheckData := map[string]interface{}{
|
|
||||||
"newtId": c.config.NewtID,
|
|
||||||
"secret": c.config.Secret,
|
|
||||||
"token": c.config.Token,
|
|
||||||
}
|
|
||||||
jsonData, err := json.Marshal(tokenCheckData)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to marshal token check data: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new request
|
|
||||||
req, err := http.NewRequest(
|
|
||||||
"POST",
|
|
||||||
baseEndpoint+"/api/v1/auth/newt/get-token",
|
|
||||||
bytes.NewBuffer(jsonData),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to create request: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set headers
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-CSRF-Token", "x-csrf-protection")
|
|
||||||
|
|
||||||
// Make the request
|
|
||||||
client := &http.Client{}
|
|
||||||
if tlsConfig != nil {
|
|
||||||
client.Transport = &http.Transport{
|
|
||||||
TLSClientConfig: tlsConfig,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to check token validity: %w", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
logger.Error("Token check failed with status code: %d", resp.StatusCode)
|
|
||||||
return "", fmt.Errorf("token check failed with status code: %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
var tokenResp TokenResponse
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil {
|
|
||||||
logger.Error("Failed to decode token check response.")
|
|
||||||
return "", fmt.Errorf("failed to decode token check response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If token is still valid, return it
|
|
||||||
if tokenResp.Success && tokenResp.Message == "Token session already valid" {
|
|
||||||
return c.config.Token, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a new token
|
// Get a new token
|
||||||
tokenData := map[string]interface{}{
|
tokenData := map[string]interface{}{
|
||||||
"newtId": c.config.NewtID,
|
"newtId": c.config.NewtID,
|
||||||
@@ -294,6 +240,8 @@ func (c *Client) getToken() (string, error) {
|
|||||||
return "", fmt.Errorf("received empty token from server")
|
return "", fmt.Errorf("received empty token from server")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Debug("Received token: %s", tokenResp.Data.Token)
|
||||||
|
|
||||||
return tokenResp.Data.Token, nil
|
return tokenResp.Data.Token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,7 +269,9 @@ func (c *Client) establishConnection() error {
|
|||||||
return fmt.Errorf("failed to get token: %w", err)
|
return fmt.Errorf("failed to get token: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.onTokenUpdate(token)
|
if c.onTokenUpdate != nil {
|
||||||
|
c.onTokenUpdate(token)
|
||||||
|
}
|
||||||
|
|
||||||
// Parse the base URL to determine protocol and hostname
|
// Parse the base URL to determine protocol and hostname
|
||||||
baseURL, err := url.Parse(c.baseURL)
|
baseURL, err := url.Parse(c.baseURL)
|
||||||
|
|||||||
@@ -48,9 +48,6 @@ func (c *Client) loadConfig() error {
|
|||||||
if c.config.NewtID == "" {
|
if c.config.NewtID == "" {
|
||||||
c.config.NewtID = config.NewtID
|
c.config.NewtID = config.NewtID
|
||||||
}
|
}
|
||||||
if c.config.Token == "" {
|
|
||||||
c.config.Token = config.Token
|
|
||||||
}
|
|
||||||
if c.config.Secret == "" {
|
if c.config.Secret == "" {
|
||||||
c.config.Secret = config.Secret
|
c.config.Secret = config.Secret
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package websocket
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
NewtID string `json:"newtId"`
|
NewtID string `json:"newtId"`
|
||||||
Secret string `json:"secret"`
|
Secret string `json:"secret"`
|
||||||
Token string `json:"token"`
|
|
||||||
Endpoint string `json:"endpoint"`
|
Endpoint string `json:"endpoint"`
|
||||||
TlsClientCert string `json:"tlsClientCert"`
|
TlsClientCert string `json:"tlsClientCert"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user