Skip to content

Commit

Permalink
2023-12-15
Browse files Browse the repository at this point in the history
  • Loading branch information
qtgolang committed Dec 14, 2023
1 parent afb088b commit 189bcc9
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 28 deletions.
12 changes: 7 additions & 5 deletions Api/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion BuildLibrary/Library/Android/arm64-v8a/libSunny.h
Original file line number Diff line number Diff line change
Expand Up @@ -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客户端 获取错误
//
Expand Down
Binary file modified BuildLibrary/Library/Android/arm64-v8a/libSunny.so
Binary file not shown.
2 changes: 1 addition & 1 deletion BuildLibrary/Library/Android/armeabi-v7a/libSunny.h
Original file line number Diff line number Diff line change
Expand Up @@ -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客户端 获取错误
//
Expand Down
Binary file modified BuildLibrary/Library/Android/armeabi-v7a/libSunny.so
Binary file not shown.
2 changes: 1 addition & 1 deletion BuildLibrary/Library/Android/x86/libSunny.h
Original file line number Diff line number Diff line change
Expand Up @@ -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客户端 获取错误
//
Expand Down
Binary file modified BuildLibrary/Library/Android/x86/libSunny.so
Binary file not shown.
Binary file modified BuildLibrary/Library/Sunny.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion BuildLibrary/Library/Sunny.h
Original file line number Diff line number Diff line change
Expand Up @@ -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客户端 获取错误
//
Expand Down
Binary file modified BuildLibrary/Library/Sunny64.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion BuildLibrary/Library/Sunny64.h
Original file line number Diff line number Diff line change
Expand Up @@ -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客户端 获取错误
//
Expand Down
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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客户端 获取错误
Expand Down
2 changes: 1 addition & 1 deletion public/constobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"
)

const SunnyVersion = "2023-12-06"
const SunnyVersion = "2023-12-15"

// TCP请求相关
const (
Expand Down
44 changes: 29 additions & 15 deletions src/websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}()

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 189bcc9

Please sign in to comment.