package shumei import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "time" ) // callDeviceAPI POSTs the encrypted payload to the Shumei device profile API // and returns the deviceId from the response. func callDeviceAPI(ctx context.Context, ep, data string) (string, error) { payload := map[string]any{ "appId": appId, "organization": organization, "ep": ep, "data": data, "os": "web", "encode": 5, "compress": 2, } body, err := json.Marshal(payload) if err != nil { return "", fmt.Errorf("marshal payload: %w", err) } url := "https://" + apiHost + apiPath req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) if err != nil { return "", fmt.Errorf("create request: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36") req.Header.Set("Origin", "https://chat.deepseek.com") req.Header.Set("Referer", "https://chat.deepseek.com/") resp, err := httpDoer.Do(req) if err != nil { return "", fmt.Errorf("http post: %w", err) } defer func() { _ = resp.Body.Close() }() respBody, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("read response: %w", err) } if resp.StatusCode != http.StatusOK { return "", fmt.Errorf("unexpected status %d: %s", resp.StatusCode, string(respBody)) } var result map[string]any if err := json.Unmarshal(respBody, &result); err != nil { return "", fmt.Errorf("parse response: %w", err) } code, _ := result["code"].(float64) if int(code) != 1100 { return "", fmt.Errorf("api error code %v: %s", result["code"], string(respBody)) } detail, _ := result["detail"].(map[string]any) deviceID, _ := detail["deviceId"].(string) if deviceID == "" { return "", fmt.Errorf("missing deviceId in response: %s", string(respBody)) } return deviceID, nil } // httpDoer is the HTTP client used for Shumei API calls. // Initialized in init() using the project's Chrome TLS transport. var httpDoer httpDoerInterface type httpDoerInterface interface { Do(req *http.Request) (*http.Response, error) } func init() { httpDoer = &http.Client{Timeout: defaultHTTPTimeout} } // SetHTTPDoer allows overriding the HTTP client (e.g. for testing or // to use the project's Chrome TLS transport). func SetHTTPDoer(d httpDoerInterface) { if d != nil { httpDoer = d } } const defaultHTTPTimeout = 15 * time.Second