52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package shadow
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
)
|
|
|
|
type Client struct {
|
|
ListenAddress string
|
|
ServerAddress string
|
|
FakeAddressSNI string
|
|
}
|
|
|
|
func NewClient(listenAddress string, serverAddress string, fakeAddressSNI string) *Client {
|
|
client := &Client{
|
|
ListenAddress: listenAddress,
|
|
ServerAddress: serverAddress,
|
|
FakeAddressSNI: fakeAddressSNI,
|
|
}
|
|
return client
|
|
}
|
|
|
|
func (c *Client) Start() {
|
|
bridge := NewTLSBridge(c.ServerAddress, c.FakeAddressSNI)
|
|
|
|
listen, err := net.Listen("tcp", c.ListenAddress)
|
|
if err != nil {
|
|
fmt.Printf("[Client] Start client error: %v\n", err)
|
|
return
|
|
}
|
|
defer listen.Close()
|
|
fmt.Printf("[Client] Listening at:%v\n", c.ListenAddress)
|
|
for {
|
|
conn, err := listen.Accept()
|
|
if err != nil {
|
|
fmt.Printf("[Client] accept error: %v\n", err)
|
|
continue
|
|
}
|
|
|
|
stream := bridge.GetStream()
|
|
if stream == nil {
|
|
conn.Close()
|
|
fmt.Println("[Client] connect to server error")
|
|
continue
|
|
}
|
|
fmt.Printf("[Client] New TCP connection: %v <-> %v \n", conn.LocalAddr().String(), conn.RemoteAddr().String())
|
|
go io.Copy(conn, stream)
|
|
go io.Copy(stream, conn)
|
|
}
|
|
}
|