(client httpclient.AuroraHttpClient, secret *tokens.Secret, state *ChatClientState, proxy string)
| 888 | } |
| 889 | |
| 890 | func DialChatWebsocketWithStateAndProxy(client httpclient.AuroraHttpClient, secret *tokens.Secret, state *ChatClientState, proxy string) (*websocket.Conn, error) { |
| 891 | wsURL, err := getChatWebsocketURLWithState(client, secret, state) |
| 892 | if err != nil { |
| 893 | return nil, err |
| 894 | } |
| 895 | dialer := websocket.Dialer{ |
| 896 | HandshakeTimeout: 15 * time.Second, |
| 897 | Proxy: fhttp.ProxyFromEnvironment, |
| 898 | } |
| 899 | if proxyFunc, err := websocketProxyFunc(proxy); err != nil { |
| 900 | return nil, err |
| 901 | } else if proxyFunc != nil { |
| 902 | dialer.Proxy = proxyFunc |
| 903 | } |
| 904 | header := fhttp.Header{} |
| 905 | ua := defaultUserAgent() |
| 906 | if state != nil && state.UserAgent != "" { |
| 907 | ua = state.UserAgent |
| 908 | } |
| 909 | header.Set("User-Agent", ua) |
| 910 | header.Set("Origin", "https://chatgpt.com") |
| 911 | conn, _, err := dialer.Dial(wsURL, header) |
| 912 | if err != nil { |
| 913 | return nil, err |
| 914 | } |
| 915 | initMsg := []map[string]interface{}{ |
| 916 | {"id": 1, "command": map[string]interface{}{ |
| 917 | "type": "connect", |
| 918 | "presence": map[string]string{"type": "presence", "state": "background"}, |
| 919 | }}, |
| 920 | {"id": 2, "command": map[string]interface{}{"type": "subscribe", "topic_id": "calpico-chatgpt"}}, |
| 921 | {"id": 3, "command": map[string]interface{}{"type": "subscribe", "topic_id": "conversations"}}, |
| 922 | {"id": 4, "command": map[string]interface{}{"type": "subscribe", "topic_id": "app_notifications"}}, |
| 923 | } |
| 924 | if err := conn.WriteJSON(initMsg); err != nil { |
| 925 | conn.Close() |
| 926 | return nil, err |
| 927 | } |
| 928 | return conn, nil |
| 929 | } |
| 930 | |
| 931 | func websocketProxyFunc(proxy string) (func(*fhttp.Request) (*url.URL, error), error) { |
| 932 | if proxy == "" { |
no test coverage detected