@@ -3,11 +3,14 @@ package edge
33import (
44 "bytes"
55 "context"
6+ "crypto/tls"
67 "encoding/json"
78 "errors"
89 "github.com/RomiChan/websocket"
910 "github.com/google/uuid"
11+ "golang.org/x/net/proxy"
1012 "io"
13+ "net"
1114 "net/http"
1215 "net/url"
1316 "strconv"
@@ -414,9 +417,25 @@ func (c *Chat) newConn() (*wsConn, error) {
414417 if err != nil {
415418 return nil , err
416419 }
417- dialer = & websocket.Dialer {
418- Proxy : http .ProxyURL (purl ),
419- HandshakeTimeout : 45 * time .Second ,
420+
421+ if purl .Scheme == "http" || purl .Scheme == "https" {
422+ dialer = & websocket.Dialer {
423+ Proxy : http .ProxyURL (purl ),
424+ HandshakeTimeout : 45 * time .Second ,
425+ }
426+ }
427+
428+ if purl .Scheme == "socks5" {
429+ dialer = & websocket.Dialer {
430+ NetDialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
431+ d , e := proxy .SOCKS5 ("tcp" , purl .Host , nil , proxy .Direct )
432+ if e != nil {
433+ return nil , e
434+ }
435+ return d .Dial (network , addr )
436+ },
437+ HandshakeTimeout : 45 * time .Second ,
438+ }
420439 }
421440 }
422441
@@ -553,7 +572,7 @@ func (c *Chat) Delete() error {
553572 }
554573
555574 request .Header = c .newHeader ()
556- client , err := c . newClient ()
575+ client , err := newClient (c . proxies )
557576 if err != nil {
558577 return & ChatError {"delete" , err }
559578 }
@@ -600,10 +619,7 @@ func (c *Chat) Delete() error {
600619 request .Header = c .newHeader ()
601620 request .Header .Set ("Authorization" , "Bearer " + authorization )
602621 request .Header .Set ("Content-Type" , "application/json" )
603- client , err = c .newClient ()
604- if err != nil {
605- return & ChatError {"delete" , err }
606- }
622+
607623 r , err = client .Do (request )
608624 if err != nil {
609625 return & ChatError {"delete" , err }
@@ -630,7 +646,7 @@ func (c *Chat) LoadPlugins(names ...string) (plugins []string, err error) {
630646 }
631647
632648 request .Header = c .newHeader ()
633- client , err := c . newClient ()
649+ client , err := newClient (c . proxies )
634650 if err != nil {
635651 return nil , err
636652 }
@@ -675,7 +691,7 @@ func (c *Chat) newConversation() (*Conversation, error) {
675691 }
676692
677693 request .Header = c .newHeader ()
678- client , err := c . newClient ()
694+ client , err := newClient (c . proxies )
679695 if err != nil {
680696 return nil , err
681697 }
@@ -715,19 +731,44 @@ func (c *Chat) newConversation() (*Conversation, error) {
715731 return & conv , nil
716732}
717733
718- func ( c * Chat ) newClient () (* http.Client , error ) {
734+ func newClient (proxies string ) (* http.Client , error ) {
719735 client := http .DefaultClient
720- if c . proxies != "" {
721- purl , err := url .Parse (c . proxies )
736+ if proxies != "" {
737+ proxiesUrl , err := url .Parse (proxies )
722738 if err != nil {
723739 return nil , err
724740 }
725- client = & http.Client {
726- Transport : & http.Transport {
727- Proxy : http .ProxyURL (purl ),
728- },
741+
742+ if proxiesUrl .Scheme == "http" || proxiesUrl .Scheme == "https" {
743+ client = & http.Client {
744+ Transport : & http.Transport {
745+ Proxy : http .ProxyURL (proxiesUrl ),
746+ TLSClientConfig : & tls.Config {
747+ InsecureSkipVerify : true ,
748+ },
749+ },
750+ }
751+ }
752+
753+ // socks5://127.0.0.1:7890
754+ if proxiesUrl .Scheme == "socks5" {
755+ client = & http.Client {
756+ Transport : & http.Transport {
757+ DialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
758+ dialer , e := proxy .SOCKS5 ("tcp" , proxiesUrl .Host , nil , proxy .Direct )
759+ if e != nil {
760+ return nil , e
761+ }
762+ return dialer .Dial (network , addr )
763+ },
764+ TLSClientConfig : & tls.Config {
765+ InsecureSkipVerify : true ,
766+ },
767+ },
768+ }
729769 }
730770 }
771+
731772 return client , nil
732773}
733774
0 commit comments