Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

Commit

Permalink
localconn
Browse files Browse the repository at this point in the history
  • Loading branch information
lzjluzijie committed Jul 10, 2018
1 parent 9fb3e4d commit 45a839e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 24 deletions.
34 changes: 10 additions & 24 deletions core/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ type WebSocksClient struct {
stopC chan int

//statistics
CreatedAt time.Time
CreatedAt time.Time
Uploaded int64
Downloaded int64
}

func NewWebSocksClient(config *WebSocksClientConfig) (client *WebSocksClient) {
Expand Down Expand Up @@ -112,7 +114,7 @@ func (client *WebSocksClient) Listen() (err error) {
break
}

go client.handleConn(conn)
go client.HandleConn(conn)
}
return nil
}
Expand All @@ -122,39 +124,23 @@ func (client *WebSocksClient) Stop() {
return
}

func (client *WebSocksClient) handleConn(conn *net.TCPConn) {
defer conn.Close()

conn.SetLinger(0)

err := handShake(conn)
if err != nil {
log.Debugf(err.Error())
return
}

_, host, err := getRequest(conn)
func (client *WebSocksClient) HandleConn(conn *net.TCPConn) {
lc, err := NewLocalConn(conn)
if err != nil {
log.Debugf(err.Error())
return
}

_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
if err != nil {
log.Debugf(err.Error())
log.Debug(err.Error())
return
}

if client.Mux {
client.DialMuxConn(host, conn)
client.DialMuxConn(lc.Host, conn)
} else {
client.DialWSConn(host, conn)
client.DialWSConn(lc.Host, lc)
}

return
}

func (client *WebSocksClient) DialWSConn(host string, conn *net.TCPConn) {
func (client *WebSocksClient) DialWSConn(host string, conn io.ReadWriter) {
wsConn, _, err := client.Dialer.Dial(client.ServerURL.String(), map[string][]string{
"WebSocks-Host": {host},
})
Expand Down
73 changes: 73 additions & 0 deletions core/client/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package client

import (
"errors"
"net"
"sync/atomic"
"time"
)

type LocalConn struct {
Host string

conn *net.TCPConn

//stats
createdAt time.Time
closed bool
readed uint64
written uint64
}

func NewLocalConn(conn *net.TCPConn) (lc *LocalConn, err error) {
conn.SetLinger(0)
err = handShake(conn)
if err != nil {
return
}

_, host, err := getRequest(conn)
if err != nil {
return
}

_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
if err != nil {
log.Debugf(err.Error())
return
}

lc = &LocalConn{
Host: host,

conn: conn,
createdAt: time.Now(),
}
return
}

func (lc *LocalConn) Read(p []byte) (n int, err error) {
if lc.closed {
return 0, errors.New("local conn closed")
}

n, err = lc.conn.Read(p)
if err != nil {
lc.closed = true
}
atomic.AddUint64(&lc.readed, uint64(n))
return
}

func (lc *LocalConn) Write(p []byte) (n int, err error) {
if lc.closed {
return 0, errors.New("local conn closed")
}

n, err = lc.conn.Write(p)
if err != nil {
lc.closed = true
}
atomic.AddUint64(&lc.written, uint64(n))
return
}

0 comments on commit 45a839e

Please sign in to comment.