diff --git a/Api/websocket.go b/Api/websocket.go index 2bd71873..923dac40 100644 --- a/Api/websocket.go +++ b/Api/websocket.go @@ -84,7 +84,7 @@ func WebsocketGetErr(Context int) uintptr { // WebsocketDial // Websocket客户端 连接 -func WebsocketDial(Context int, URL, Heads string, call int, synchronous bool, ProxyUrl string, CertificateConText int) bool { +func WebsocketDial(Context int, URL, Heads string, call int, synchronous bool, ProxyUrl string, CertificateConText int, outTime int) bool { w := LoadWebSocketContext(Context) if w == nil { return false @@ -95,14 +95,15 @@ func WebsocketDial(Context int, URL, Heads string, call int, synchronous bool, P w.err = nil head := strings.ReplaceAll(Heads, "\r", "") var dialer websocket.Dialer - Request, _ := http.NewRequest("GET", strings.Replace(URL, "wss://", "https://", 1), nil) + //Request, _ := http.NewRequest("GET", strings.Replace(URL, "wss://", "https://", 1), nil) + Header := make(http.Header) arr := strings.Split(head, "\n") for _, v := range arr { arr1 := strings.Split(v, ":") if len(arr1) >= 2 { k := arr1[0] val := strings.TrimSpace(strings.Replace(v, arr1[0]+":", "", 1)) - Request.Header[textproto.TrimString(k)] = []string{val} + Header[textproto.TrimString(k)] = []string{val} } } mUrl := strings.ToLower(URL) @@ -130,8 +131,9 @@ func WebsocketDial(Context int, URL, Heads string, call int, synchronous bool, P if len(a.Host) < 3 { Proxy_ = "" } - w.wb, _, w.err = dialer.ConnDialContext(Request, Proxy_) - //w.wb, _, w.err = dialer.Dial(URL, Headers, Proxy_) + //w.wb, _, w.err = dialer.Dial(Request.URL.String(), Request.Header, Proxy_) + //w.wb, _, w.err = dialer.ConnDialContext(Request, Proxy_) + w.wb, _, w.err = dialer.Dial(URL, Header, Proxy_, outTime) if w.err != nil { return false } diff --git a/BuildLibrary/Library/Android/arm64-v8a/libSunny.h b/BuildLibrary/Library/Android/arm64-v8a/libSunny.h index 566835b4..b796173f 100644 --- a/BuildLibrary/Library/Android/arm64-v8a/libSunny.h +++ b/BuildLibrary/Library/Android/arm64-v8a/libSunny.h @@ -679,7 +679,7 @@ extern void WebsocketClose(GoInt Context); // Websocket客户端 连接 // -extern GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText); +extern GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText, GoInt outTime); // Websocket客户端 获取错误 // diff --git a/BuildLibrary/Library/Android/arm64-v8a/libSunny.so b/BuildLibrary/Library/Android/arm64-v8a/libSunny.so index d068f9ac..17d833e3 100644 Binary files a/BuildLibrary/Library/Android/arm64-v8a/libSunny.so and b/BuildLibrary/Library/Android/arm64-v8a/libSunny.so differ diff --git a/BuildLibrary/Library/Android/armeabi-v7a/libSunny.h b/BuildLibrary/Library/Android/armeabi-v7a/libSunny.h index e01c135c..ecf74478 100644 --- a/BuildLibrary/Library/Android/armeabi-v7a/libSunny.h +++ b/BuildLibrary/Library/Android/armeabi-v7a/libSunny.h @@ -679,7 +679,7 @@ extern void WebsocketClose(GoInt Context); // Websocket客户端 连接 // -extern GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText); +extern GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText, GoInt outTime); // Websocket客户端 获取错误 // diff --git a/BuildLibrary/Library/Android/armeabi-v7a/libSunny.so b/BuildLibrary/Library/Android/armeabi-v7a/libSunny.so index 6c0aeaad..37334f9c 100644 Binary files a/BuildLibrary/Library/Android/armeabi-v7a/libSunny.so and b/BuildLibrary/Library/Android/armeabi-v7a/libSunny.so differ diff --git a/BuildLibrary/Library/Android/x86/libSunny.h b/BuildLibrary/Library/Android/x86/libSunny.h index e01c135c..ecf74478 100644 --- a/BuildLibrary/Library/Android/x86/libSunny.h +++ b/BuildLibrary/Library/Android/x86/libSunny.h @@ -679,7 +679,7 @@ extern void WebsocketClose(GoInt Context); // Websocket客户端 连接 // -extern GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText); +extern GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText, GoInt outTime); // Websocket客户端 获取错误 // diff --git a/BuildLibrary/Library/Android/x86/libSunny.so b/BuildLibrary/Library/Android/x86/libSunny.so index 8ec115c1..4baaf478 100644 Binary files a/BuildLibrary/Library/Android/x86/libSunny.so and b/BuildLibrary/Library/Android/x86/libSunny.so differ diff --git a/BuildLibrary/Library/Sunny.dll b/BuildLibrary/Library/Sunny.dll index 90a13165..151898a3 100644 Binary files a/BuildLibrary/Library/Sunny.dll and b/BuildLibrary/Library/Sunny.dll differ diff --git a/BuildLibrary/Library/Sunny.h b/BuildLibrary/Library/Sunny.h index 8da16dbc..80998195 100644 --- a/BuildLibrary/Library/Sunny.h +++ b/BuildLibrary/Library/Sunny.h @@ -679,7 +679,7 @@ extern __declspec(dllexport) void WebsocketClose(GoInt Context); // Websocket客户端 连接 // -extern __declspec(dllexport) GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText); +extern __declspec(dllexport) GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText, GoInt outTime); // Websocket客户端 获取错误 // diff --git a/BuildLibrary/Library/Sunny64.dll b/BuildLibrary/Library/Sunny64.dll index c60a948c..e48334fe 100644 Binary files a/BuildLibrary/Library/Sunny64.dll and b/BuildLibrary/Library/Sunny64.dll differ diff --git a/BuildLibrary/Library/Sunny64.h b/BuildLibrary/Library/Sunny64.h index e8107388..56900239 100644 --- a/BuildLibrary/Library/Sunny64.h +++ b/BuildLibrary/Library/Sunny64.h @@ -679,7 +679,7 @@ extern __declspec(dllexport) void WebsocketClose(GoInt Context); // Websocket客户端 连接 // -extern __declspec(dllexport) GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText); +extern __declspec(dllexport) GoUint8 WebsocketDial(GoInt Context, char* URL, char* Heads, GoInt call, GoUint8 synchronous, char* ProxyUrl, GoInt CertificateConText, GoInt outTime); // Websocket客户端 获取错误 // diff --git a/api.go b/api.go index c1997584..0dc235ae 100644 --- a/api.go +++ b/api.go @@ -1078,8 +1078,8 @@ func WebsocketClose(Context int) { // Websocket客户端 连接 // //export WebsocketDial -func WebsocketDial(Context int, URL, Heads *C.char, call int, synchronous bool, ProxyUrl *C.char, CertificateConText int) bool { - return Api.WebsocketDial(Context, C.GoString(URL), C.GoString(Heads), call, synchronous, C.GoString(ProxyUrl), CertificateConText) +func WebsocketDial(Context int, URL, Heads *C.char, call int, synchronous bool, ProxyUrl *C.char, CertificateConText, outTime int) bool { + return Api.WebsocketDial(Context, C.GoString(URL), C.GoString(Heads), call, synchronous, C.GoString(ProxyUrl), CertificateConText, outTime) } // Websocket客户端 获取错误 diff --git a/public/constobj.go b/public/constobj.go index 954f7cf2..0927bac0 100644 --- a/public/constobj.go +++ b/public/constobj.go @@ -13,7 +13,7 @@ import ( "time" ) -const SunnyVersion = "2023-12-06" +const SunnyVersion = "2023-12-15" // TCP请求相关 const ( diff --git a/src/websocket/client.go b/src/websocket/client.go index cd021877..4868f8c8 100644 --- a/src/websocket/client.go +++ b/src/websocket/client.go @@ -103,9 +103,13 @@ type Dialer struct { ProxyUrl string } -func (d *Dialer) Dial(urlStr string, requestHeader http.Header, ProxyUrl string) (*Conn, *http.Response, error) { +func (d *Dialer) Dial(urlStr string, requestHeader http.Header, ProxyUrl string, outTime ...int) (*Conn, *http.Response, error) { d.ProxyUrl = ProxyUrl - return d.DialContext(context.Background(), urlStr, requestHeader) + t := 0 + if len(outTime) > 0 { + t = outTime[0] + } + return d.DialContext(context.Background(), urlStr, requestHeader, t) } var errMalformedURL = errors.New("malformed ws or wss URL") @@ -148,7 +152,7 @@ var nilDialer = *DefaultDialer // non-nil *net.Response so that callers can handle redirects, authentication, // etcetera. The response body may not contain the entire response and does not // need to be closed by the application. -func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) { +func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header, outTime ...int) (*Conn, *http.Response, error) { if d == nil { d = &nilDialer } @@ -222,18 +226,20 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h // Get network dial function. var netDial func(network, add string) (net.Conn, error) - if d.NetDialContext != nil { - netDial = func(network, addr string) (net.Conn, error) { - return d.NetDialContext(ctx, network, addr) - } - } else if d.NetDial != nil { - netDial = d.NetDial - } else { - netDialer := &net.Dialer{} - netDial = func(network, addr string) (net.Conn, error) { - return netDialer.DialContext(ctx, network, addr) + netDialer := &net.Dialer{} + _outTime := 3000 + if len(outTime) > 0 { + t := outTime[0] + if t > 10 { + _outTime = t + netDialer.Timeout = time.Duration(t) * time.Millisecond + } else { + netDialer.Timeout = 30000 * time.Millisecond } } + netDial = func(network, addr string) (net.Conn, error) { + return netDialer.DialContext(ctx, network, addr) + } // If needed, wrap the dial function to set the connection deadline. if deadline, ok := ctx.Deadline(); ok { @@ -288,10 +294,9 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h if err != nil { return nil, nil, err } - defer func() { if netConn != nil { - netConn.Close() + _ = netConn.Close() } }() @@ -327,6 +332,15 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h return nil, nil, err } + _ = conn.conn.SetDeadline(time.Now().Add(time.Duration(_outTime) * time.Millisecond)) + defer func() { + if conn != nil { + if conn.conn != nil { + _ = conn.conn.SetDeadline(time.Time{}) + } + } + }() + if trace != nil && trace.GotFirstResponseByte != nil { if peek, err := conn.br.Peek(1); err == nil && len(peek) == 1 { trace.GotFirstResponseByte()