client.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package main
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "crypto/ed25519"
  6. "crypto/rand"
  7. "encoding/json"
  8. "encoding/pem"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "os"
  13. "time"
  14. "golang.org/x/crypto/ssh"
  15. )
  16. // APIClient handles communication with the server
  17. type APIClient struct {
  18. baseURL string
  19. token string
  20. httpClient *http.Client
  21. }
  22. // NewAPIClient creates a new API client
  23. func NewAPIClient(baseURL string) *APIClient {
  24. return &APIClient{
  25. baseURL: baseURL,
  26. httpClient: &http.Client{
  27. Timeout: 30 * time.Second,
  28. },
  29. }
  30. }
  31. // SetToken sets the authentication token
  32. func (c *APIClient) SetToken(token string) {
  33. c.token = token
  34. }
  35. // RegistrationRequest is sent to register a device
  36. type RegistrationRequest struct {
  37. DeviceID string `json:"device_id"`
  38. EthIP *string `json:"eth_ip,omitempty"`
  39. WlanIP *string `json:"wlan_ip,omitempty"`
  40. SSHPublicKey string `json:"ssh_public_key,omitempty"`
  41. }
  42. // RegistrationResponse is returned from registration
  43. type RegistrationResponse struct {
  44. DeviceToken string `json:"device_token"`
  45. DevicePassword string `json:"device_password,omitempty"`
  46. SSHTunnel struct {
  47. Enabled bool `json:"enabled"`
  48. RemotePort int `json:"remote_port"`
  49. Server string `json:"server"`
  50. } `json:"ssh_tunnel,omitempty"`
  51. }
  52. // Register registers a device with the server
  53. func (c *APIClient) Register(req *RegistrationRequest) (*RegistrationResponse, error) {
  54. body, err := json.Marshal(req)
  55. if err != nil {
  56. return nil, err
  57. }
  58. httpReq, err := http.NewRequest("POST", c.baseURL+"/registration", bytes.NewReader(body))
  59. if err != nil {
  60. return nil, err
  61. }
  62. httpReq.Header.Set("Content-Type", "application/json")
  63. resp, err := c.httpClient.Do(httpReq)
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer resp.Body.Close()
  68. if resp.StatusCode != 201 && resp.StatusCode != 200 {
  69. respBody, _ := io.ReadAll(resp.Body)
  70. return nil, fmt.Errorf("registration failed: %d %s", resp.StatusCode, string(respBody))
  71. }
  72. var regResp RegistrationResponse
  73. if err := json.NewDecoder(resp.Body).Decode(&regResp); err != nil {
  74. return nil, err
  75. }
  76. return &regResp, nil
  77. }
  78. // ServerConfig is the configuration returned by the server
  79. type ServerConfig struct {
  80. // ForceCloud overrides local mode setting - for remote support
  81. ForceCloud bool `json:"force_cloud"`
  82. BLE struct {
  83. Enabled bool `json:"enabled"`
  84. BatchIntervalMs int `json:"batch_interval_ms"`
  85. UUIDFilterHex string `json:"uuid_filter_hex,omitempty"`
  86. } `json:"ble"`
  87. WiFi struct {
  88. MonitorEnabled bool `json:"monitor_enabled"`
  89. ClientEnabled bool `json:"client_enabled"`
  90. SSID string `json:"ssid"`
  91. PSK string `json:"psk"`
  92. BatchIntervalMs int `json:"batch_interval_ms"`
  93. } `json:"wifi"`
  94. SSHTunnel struct {
  95. Enabled bool `json:"enabled"`
  96. Server string `json:"server"`
  97. Port int `json:"port"`
  98. User string `json:"user"`
  99. RemotePort int `json:"remote_port"`
  100. KeepaliveInterval int `json:"keepalive_interval"`
  101. } `json:"ssh_tunnel"`
  102. DashboardTunnel struct {
  103. Enabled bool `json:"enabled"`
  104. Server string `json:"server"`
  105. Port int `json:"port"`
  106. User string `json:"user"`
  107. RemotePort int `json:"remote_port"`
  108. KeepaliveInterval int `json:"keepalive_interval"`
  109. } `json:"dashboard_tunnel"`
  110. Dashboard struct {
  111. Enabled bool `json:"enabled"`
  112. } `json:"dashboard"`
  113. Net struct {
  114. NTP struct {
  115. Servers []string `json:"servers"`
  116. } `json:"ntp"`
  117. } `json:"net"`
  118. Debug bool `json:"debug"`
  119. }
  120. // GetConfig fetches configuration from the server
  121. func (c *APIClient) GetConfig(deviceID string) (*ServerConfig, error) {
  122. httpReq, err := http.NewRequest("GET", c.baseURL+"/config", nil)
  123. if err != nil {
  124. return nil, err
  125. }
  126. q := httpReq.URL.Query()
  127. q.Set("device_id", deviceID)
  128. httpReq.URL.RawQuery = q.Encode()
  129. if c.token != "" {
  130. httpReq.Header.Set("Authorization", "Bearer "+c.token)
  131. }
  132. resp, err := c.httpClient.Do(httpReq)
  133. if err != nil {
  134. return nil, err
  135. }
  136. defer resp.Body.Close()
  137. if resp.StatusCode != 200 {
  138. return nil, fmt.Errorf("config fetch failed: %d", resp.StatusCode)
  139. }
  140. var cfg ServerConfig
  141. if err := json.NewDecoder(resp.Body).Decode(&cfg); err != nil {
  142. return nil, err
  143. }
  144. return &cfg, nil
  145. }
  146. // EventBatch is a batch of events to upload
  147. type EventBatch struct {
  148. DeviceID string `json:"device_id"`
  149. Events []interface{} `json:"events"`
  150. }
  151. // UploadEvents uploads a batch of events (gzipped)
  152. func (c *APIClient) UploadEvents(endpoint string, batch *EventBatch) error {
  153. // Serialize to JSON
  154. jsonData, err := json.Marshal(batch)
  155. if err != nil {
  156. return err
  157. }
  158. // Gzip compress
  159. var buf bytes.Buffer
  160. gw := gzip.NewWriter(&buf)
  161. if _, err := gw.Write(jsonData); err != nil {
  162. return err
  163. }
  164. if err := gw.Close(); err != nil {
  165. return err
  166. }
  167. // Store compressed data for retries
  168. compressedData := buf.Bytes()
  169. url := c.baseURL + endpoint
  170. // Send with retries
  171. var lastErr error
  172. for attempt := 1; attempt <= 3; attempt++ {
  173. // Create fresh request for each attempt (body can only be read once)
  174. httpReq, err := http.NewRequest("POST", url, bytes.NewReader(compressedData))
  175. if err != nil {
  176. return err
  177. }
  178. httpReq.Header.Set("Content-Type", "application/json")
  179. httpReq.Header.Set("Content-Encoding", "gzip")
  180. if c.token != "" {
  181. httpReq.Header.Set("Authorization", "Bearer "+c.token)
  182. }
  183. resp, err := c.httpClient.Do(httpReq)
  184. if err != nil {
  185. lastErr = err
  186. time.Sleep(time.Duration(attempt) * time.Second)
  187. continue
  188. }
  189. resp.Body.Close()
  190. if resp.StatusCode >= 200 && resp.StatusCode < 300 {
  191. return nil
  192. }
  193. lastErr = fmt.Errorf("upload failed: %d", resp.StatusCode)
  194. time.Sleep(time.Duration(attempt) * time.Second)
  195. }
  196. return lastErr
  197. }
  198. // WiFiCredentialsUpdate is the request to update WiFi credentials on server
  199. type WiFiCredentialsUpdate struct {
  200. SSID string `json:"ssid"`
  201. PSK string `json:"psk"`
  202. }
  203. // UpdateWiFiCredentials sends WiFi credentials to server (Cloud Mode only)
  204. func (c *APIClient) UpdateWiFiCredentials(ssid, psk string) error {
  205. body, err := json.Marshal(&WiFiCredentialsUpdate{
  206. SSID: ssid,
  207. PSK: psk,
  208. })
  209. if err != nil {
  210. return err
  211. }
  212. httpReq, err := http.NewRequest("POST", c.baseURL+"/wifi-credentials", bytes.NewReader(body))
  213. if err != nil {
  214. return err
  215. }
  216. httpReq.Header.Set("Content-Type", "application/json")
  217. if c.token != "" {
  218. httpReq.Header.Set("Authorization", "Bearer "+c.token)
  219. }
  220. resp, err := c.httpClient.Do(httpReq)
  221. if err != nil {
  222. return err
  223. }
  224. defer resp.Body.Close()
  225. if resp.StatusCode != 200 && resp.StatusCode != 201 {
  226. respBody, _ := io.ReadAll(resp.Body)
  227. return fmt.Errorf("wifi credentials update failed: %d %s", resp.StatusCode, string(respBody))
  228. }
  229. return nil
  230. }
  231. // GenerateOrLoadSSHKey generates ED25519 key pair or loads existing one
  232. // Returns OpenSSH public key format
  233. func GenerateOrLoadSSHKey(keyPath string) (string, error) {
  234. // Check if key already exists
  235. if _, err := os.Stat(keyPath); err == nil {
  236. // Load existing key
  237. privKeyBytes, err := os.ReadFile(keyPath)
  238. if err != nil {
  239. return "", fmt.Errorf("failed to read existing key: %w", err)
  240. }
  241. block, _ := pem.Decode(privKeyBytes)
  242. if block == nil {
  243. return "", fmt.Errorf("failed to decode PEM block")
  244. }
  245. // Parse ED25519 private key
  246. privKey, err := ssh.ParseRawPrivateKey(privKeyBytes)
  247. if err != nil {
  248. return "", fmt.Errorf("failed to parse private key: %w", err)
  249. }
  250. ed25519Key, ok := privKey.(ed25519.PrivateKey)
  251. if !ok {
  252. return "", fmt.Errorf("key is not ED25519")
  253. }
  254. // Extract public key
  255. pubKey := ed25519Key.Public().(ed25519.PublicKey)
  256. sshPubKey, err := ssh.NewPublicKey(pubKey)
  257. if err != nil {
  258. return "", fmt.Errorf("failed to create SSH public key: %w", err)
  259. }
  260. return string(ssh.MarshalAuthorizedKey(sshPubKey)), nil
  261. }
  262. // Generate new ED25519 key pair
  263. pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
  264. if err != nil {
  265. return "", fmt.Errorf("failed to generate ED25519 key: %w", err)
  266. }
  267. // Convert to OpenSSH format for private key
  268. privKeyPEM, err := ssh.MarshalPrivateKey(privKey, "")
  269. if err != nil {
  270. return "", fmt.Errorf("failed to marshal private key: %w", err)
  271. }
  272. // Save private key
  273. privKeyFile, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  274. if err != nil {
  275. return "", fmt.Errorf("failed to create private key file: %w", err)
  276. }
  277. defer privKeyFile.Close()
  278. if err := pem.Encode(privKeyFile, privKeyPEM); err != nil {
  279. return "", fmt.Errorf("failed to write private key: %w", err)
  280. }
  281. // Convert public key to OpenSSH format
  282. sshPubKey, err := ssh.NewPublicKey(pubKey)
  283. if err != nil {
  284. return "", fmt.Errorf("failed to create SSH public key: %w", err)
  285. }
  286. pubKeyStr := string(ssh.MarshalAuthorizedKey(sshPubKey))
  287. // Save public key
  288. pubKeyPath := keyPath + ".pub"
  289. if err := os.WriteFile(pubKeyPath, []byte(pubKeyStr), 0644); err != nil {
  290. return "", fmt.Errorf("failed to write public key: %w", err)
  291. }
  292. return pubKeyStr, nil
  293. }