client.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. Dashboard struct {
  103. Enabled bool `json:"enabled"`
  104. } `json:"dashboard"`
  105. Net struct {
  106. NTP struct {
  107. Servers []string `json:"servers"`
  108. } `json:"ntp"`
  109. } `json:"net"`
  110. Debug bool `json:"debug"`
  111. }
  112. // GetConfig fetches configuration from the server
  113. func (c *APIClient) GetConfig(deviceID string) (*ServerConfig, error) {
  114. httpReq, err := http.NewRequest("GET", c.baseURL+"/config", nil)
  115. if err != nil {
  116. return nil, err
  117. }
  118. q := httpReq.URL.Query()
  119. q.Set("device_id", deviceID)
  120. httpReq.URL.RawQuery = q.Encode()
  121. if c.token != "" {
  122. httpReq.Header.Set("Authorization", "Bearer "+c.token)
  123. }
  124. resp, err := c.httpClient.Do(httpReq)
  125. if err != nil {
  126. return nil, err
  127. }
  128. defer resp.Body.Close()
  129. if resp.StatusCode != 200 {
  130. return nil, fmt.Errorf("config fetch failed: %d", resp.StatusCode)
  131. }
  132. var cfg ServerConfig
  133. if err := json.NewDecoder(resp.Body).Decode(&cfg); err != nil {
  134. return nil, err
  135. }
  136. return &cfg, nil
  137. }
  138. // EventBatch is a batch of events to upload
  139. type EventBatch struct {
  140. DeviceID string `json:"device_id"`
  141. Events []interface{} `json:"events"`
  142. }
  143. // UploadEvents uploads a batch of events (gzipped)
  144. func (c *APIClient) UploadEvents(endpoint string, batch *EventBatch) error {
  145. // Serialize to JSON
  146. jsonData, err := json.Marshal(batch)
  147. if err != nil {
  148. return err
  149. }
  150. // Gzip compress
  151. var buf bytes.Buffer
  152. gw := gzip.NewWriter(&buf)
  153. if _, err := gw.Write(jsonData); err != nil {
  154. return err
  155. }
  156. if err := gw.Close(); err != nil {
  157. return err
  158. }
  159. // Store compressed data for retries
  160. compressedData := buf.Bytes()
  161. url := c.baseURL + endpoint
  162. // Send with retries
  163. var lastErr error
  164. for attempt := 1; attempt <= 3; attempt++ {
  165. // Create fresh request for each attempt (body can only be read once)
  166. httpReq, err := http.NewRequest("POST", url, bytes.NewReader(compressedData))
  167. if err != nil {
  168. return err
  169. }
  170. httpReq.Header.Set("Content-Type", "application/json")
  171. httpReq.Header.Set("Content-Encoding", "gzip")
  172. if c.token != "" {
  173. httpReq.Header.Set("Authorization", "Bearer "+c.token)
  174. }
  175. resp, err := c.httpClient.Do(httpReq)
  176. if err != nil {
  177. lastErr = err
  178. time.Sleep(time.Duration(attempt) * time.Second)
  179. continue
  180. }
  181. resp.Body.Close()
  182. if resp.StatusCode >= 200 && resp.StatusCode < 300 {
  183. return nil
  184. }
  185. lastErr = fmt.Errorf("upload failed: %d", resp.StatusCode)
  186. time.Sleep(time.Duration(attempt) * time.Second)
  187. }
  188. return lastErr
  189. }
  190. // WiFiCredentialsUpdate is the request to update WiFi credentials on server
  191. type WiFiCredentialsUpdate struct {
  192. SSID string `json:"ssid"`
  193. PSK string `json:"psk"`
  194. }
  195. // UpdateWiFiCredentials sends WiFi credentials to server (Cloud Mode only)
  196. func (c *APIClient) UpdateWiFiCredentials(ssid, psk string) error {
  197. body, err := json.Marshal(&WiFiCredentialsUpdate{
  198. SSID: ssid,
  199. PSK: psk,
  200. })
  201. if err != nil {
  202. return err
  203. }
  204. httpReq, err := http.NewRequest("POST", c.baseURL+"/wifi-credentials", bytes.NewReader(body))
  205. if err != nil {
  206. return err
  207. }
  208. httpReq.Header.Set("Content-Type", "application/json")
  209. if c.token != "" {
  210. httpReq.Header.Set("Authorization", "Bearer "+c.token)
  211. }
  212. resp, err := c.httpClient.Do(httpReq)
  213. if err != nil {
  214. return err
  215. }
  216. defer resp.Body.Close()
  217. if resp.StatusCode != 200 && resp.StatusCode != 201 {
  218. respBody, _ := io.ReadAll(resp.Body)
  219. return fmt.Errorf("wifi credentials update failed: %d %s", resp.StatusCode, string(respBody))
  220. }
  221. return nil
  222. }
  223. // GenerateOrLoadSSHKey generates ED25519 key pair or loads existing one
  224. // Returns OpenSSH public key format
  225. func GenerateOrLoadSSHKey(keyPath string) (string, error) {
  226. // Check if key already exists
  227. if _, err := os.Stat(keyPath); err == nil {
  228. // Load existing key
  229. privKeyBytes, err := os.ReadFile(keyPath)
  230. if err != nil {
  231. return "", fmt.Errorf("failed to read existing key: %w", err)
  232. }
  233. block, _ := pem.Decode(privKeyBytes)
  234. if block == nil {
  235. return "", fmt.Errorf("failed to decode PEM block")
  236. }
  237. // Parse ED25519 private key
  238. privKey, err := ssh.ParseRawPrivateKey(privKeyBytes)
  239. if err != nil {
  240. return "", fmt.Errorf("failed to parse private key: %w", err)
  241. }
  242. ed25519Key, ok := privKey.(ed25519.PrivateKey)
  243. if !ok {
  244. return "", fmt.Errorf("key is not ED25519")
  245. }
  246. // Extract public key
  247. pubKey := ed25519Key.Public().(ed25519.PublicKey)
  248. sshPubKey, err := ssh.NewPublicKey(pubKey)
  249. if err != nil {
  250. return "", fmt.Errorf("failed to create SSH public key: %w", err)
  251. }
  252. return string(ssh.MarshalAuthorizedKey(sshPubKey)), nil
  253. }
  254. // Generate new ED25519 key pair
  255. pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
  256. if err != nil {
  257. return "", fmt.Errorf("failed to generate ED25519 key: %w", err)
  258. }
  259. // Convert to OpenSSH format for private key
  260. privKeyPEM, err := ssh.MarshalPrivateKey(privKey, "")
  261. if err != nil {
  262. return "", fmt.Errorf("failed to marshal private key: %w", err)
  263. }
  264. // Save private key
  265. privKeyFile, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  266. if err != nil {
  267. return "", fmt.Errorf("failed to create private key file: %w", err)
  268. }
  269. defer privKeyFile.Close()
  270. if err := pem.Encode(privKeyFile, privKeyPEM); err != nil {
  271. return "", fmt.Errorf("failed to write private key: %w", err)
  272. }
  273. // Convert public key to OpenSSH format
  274. sshPubKey, err := ssh.NewPublicKey(pubKey)
  275. if err != nil {
  276. return "", fmt.Errorf("failed to create SSH public key: %w", err)
  277. }
  278. pubKeyStr := string(ssh.MarshalAuthorizedKey(sshPubKey))
  279. // Save public key
  280. pubKeyPath := keyPath + ".pub"
  281. if err := os.WriteFile(pubKeyPath, []byte(pubKeyStr), 0644); err != nil {
  282. return "", fmt.Errorf("failed to write public key: %w", err)
  283. }
  284. return pubKeyStr, nil
  285. }