diff --git a/protocol/protocol.go b/protocol/protocol.go index f581657..011c117 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -332,6 +332,26 @@ func (p *Protocol) processRequest(conn net.Conn, message string) { } log.Info(fmt.Sprintf("%sConnected client version: '%s' [set KeepDaemonAlone = %t]", p.connLogID(conn), req.Version, req.KeepDaemonAlone)) + + // When upgrading from old client version, it is necessary to copy current credentials from UI client + if len(req.SetRawCredentials.AccountID) > 0 && len(req.SetRawCredentials.Session) > 0 { + r := req.SetRawCredentials + if err := p._service.SetRawCredentials(r.AccountID, + r.Session, + r.OvpnUser, + r.OvpnPass, + r.WgPublicKey, + r.WgPrivateKey, + r.WgLocalIP, + r.WgKeyGenerated); err != nil { + // failed to save RAW credentials + err := fmt.Errorf("failed to register RAW credentials: %w", err) + log.Error(err) + p.sendErrorResponse(conn, reqCmd, err) + return + } + } + // send back Hello message with account session info p.sendResponse(conn, p.createHelloResponse(), req.Idx) @@ -560,28 +580,6 @@ func (p *Protocol) processRequest(conn net.Conn, message string) { p.sendResponse(conn, &types.EmptyResp{}, reqCmd.Idx) break - case "SetCredentials": - var r types.SetCredentials - if err := json.Unmarshal(messageData, &r); err != nil { - p.sendErrorResponse(conn, reqCmd, err) - break - } - - if err := p._service.SetRawCredentials(r.AccountID, - r.Session, - r.OvpnUser, - r.OvpnPass, - r.WgPublicKey, - r.WgPrivateKey, - r.WgLocalIP, - r.WgKeyGenerated); err != nil { - p.sendErrorResponse(conn, reqCmd, err) - } else { - p.sendResponse(conn, &types.EmptyResp{}, reqCmd.Idx) - } - - break - case "SessionNew": var req types.SessionNew if err := json.Unmarshal(messageData, &req); err != nil { diff --git a/protocol/types/requests.go b/protocol/types/requests.go index a6e1039..c829e01 100644 --- a/protocol/types/requests.go +++ b/protocol/types/requests.go @@ -18,6 +18,22 @@ type Hello struct { // KeepDaemonAlone == false (default) - VPN disconnects when client disconnects from a daemon // KeepDaemonAlone == true - do nothing when client disconnects from a daemon (if VPN is connected - do not disconnect) KeepDaemonAlone bool + + // Register credentials (if not logged in) + // Used when updating from an old client version + SetRawCredentials RawCredentials +} + +// RawCredentials - RAW credentials +type RawCredentials struct { + AccountID string + Session string + OvpnUser string + OvpnPass string + WgPublicKey string + WgPrivateKey string + WgLocalIP string + WgKeyGenerated int64 // Unix time } // GetServers request servers list @@ -167,16 +183,3 @@ type WireGuardSetKeysRotationInterval struct { CommandBase Interval int64 } - -// SetCredentials - manually set RAW credentials -type SetCredentials struct { - CommandBase - AccountID string - Session string - OvpnUser string - OvpnPass string - WgPublicKey string - WgPrivateKey string - WgLocalIP string - WgKeyGenerated int64 // Unix time -}