func NewClient(originURL, targetURL string, settings map[string]interface{}) (c *websocket.Conn, response *http.Response, err error) { u, err := url.Parse(targetURL) if err != nil { return nil, nil, err } dialer := net.Dialer{} timeoutInterface, ok := settings["Timeout"] if ok { dialer.Timeout = timeoutInterface.(time.Duration) } keepAliveInterface, ok := settings["KeepAlive"] if ok { dialer.KeepAlive = keepAliveInterface.(time.Duration) } rawConn, err := dialer.Dial("tcp", u.Host) if err != nil { logrus.WithFields(logrus.Fields{ "Error": err.Error(), }).Errorf("TCP connection error when dialing %v", u.Host) return nil, nil, err } wsHeaders := http.Header{ "Origin": {originURL}, // your milage may differ "Sec-WebSocket-Extensions": {"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"}, } return websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024) }
func ConnectSrv(Config ConfigT) net.Conn { var dialer net.Dialer dialer.Timeout = time.Second dialer.KeepAlive = time.Minute chandesc, err := dialer.Dial("tcp", ConcatStr(":", Config.Main.Server, Config.Main.Port)) if err != nil { LogErr(err, "serv dial") return nil } else { var send string = Config.Main.CCID for _, target := range Config.Main.TargetID { send = ConcatStr(" ", send, target) } fmt.Fprintf(chandesc, "%s", send) return chandesc } }
func Dial(_s Stream) (err error) { switch w := _s.(type) { case *wrapperStream: switch s := w.base.(type) { case *dummyStream: x := &xmppStream{to: s.to, onCloseFunc: w.onErrorFunc} var ( host, port string dial net.Dialer ) if host, port, err = srv.Resolve(x.to); err == nil { dial.KeepAlive = 10 * time.Second if x.conn, err = dial.Dial("tcp", host+":"+port); err == nil { x.ctrl = make(chan bool) x.data = make(chan pack, 256) go func(stream *xmppStream) { <-stream.ctrl stream.conn.Close() }(x) doSplit := func(stream *xmppStream, onErr func(error)) { for data := range spl1t(stream.conn, onErr) { //log.Println("SPLIT") //log.Println(string(data)) //log.Println() stream.data <- pack{data: data, hash: adler32.Checksum(data)} } } var onErr func(error) onErr = func(_err error) { switch e := _err.(type) { case net.Error: if e.Temporary() { go doSplit(x, onErr) } else { x.closeStream() x.onClose(e) w.base = s } default: panic(fmt.Sprint(reflect.TypeOf(e), e)) } } go doSplit(x, onErr) w.base = x } else { x.onClose(err) } } else { x.onClose(err) } default: err = errors.New("already connected") } default: halt.As(100, reflect.TypeOf(_s)) } return }