add go module file and test
parent
804836b7cb
commit
10850b7f92
@ -0,0 +1,14 @@
|
|||||||
|
module github.com/fangdingjun/sniproxy
|
||||||
|
|
||||||
|
go 1.13
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/fangdingjun/go-log v4.0.2+incompatible
|
||||||
|
github.com/fangdingjun/protolistener v0.0.0-20190415072226-223d4e06b774
|
||||||
|
github.com/kr/pretty v0.1.0 // indirect
|
||||||
|
github.com/kr/pty v1.1.4 // indirect
|
||||||
|
github.com/pires/go-proxyproto v0.0.0-20190111085350-4d51b51e3bfc
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.2.2
|
||||||
|
)
|
@ -0,0 +1,31 @@
|
|||||||
|
github.com/fangdingjun/go-log v4.0.0+incompatible h1:h/3S0FRNiFR1A5xkK/ECHtmc2O/DVESKCzjhpgzszfA=
|
||||||
|
github.com/fangdingjun/go-log v4.0.0+incompatible/go.mod h1:Y6ko/KySCpIcAX3mrfiUJtuPY2saPwofv9WYcUatPJY=
|
||||||
|
github.com/fangdingjun/go-log v4.0.1+incompatible h1:g/0h/FPwSQVGESR+hm50xSdGdLIXKSR+M8CnTjIlZfE=
|
||||||
|
github.com/fangdingjun/go-log v4.0.1+incompatible/go.mod h1:Y6ko/KySCpIcAX3mrfiUJtuPY2saPwofv9WYcUatPJY=
|
||||||
|
github.com/fangdingjun/go-log v4.0.2+incompatible h1:nUSCJTxuGP8493r8GtGQ2xyWw6/DzuAzDBg1opfSKT8=
|
||||||
|
github.com/fangdingjun/go-log v4.0.2+incompatible/go.mod h1:Y6ko/KySCpIcAX3mrfiUJtuPY2saPwofv9WYcUatPJY=
|
||||||
|
github.com/fangdingjun/protolistener v0.0.0-20190413090244-355464816fc0 h1:dafx8KP2yvsX8oAFwJ6Bx54h3YE2P7UXLB453z7LbPo=
|
||||||
|
github.com/fangdingjun/protolistener v0.0.0-20190413090244-355464816fc0/go.mod h1:bp6oYi3nsUwqGyzikoHJ672i9vX39Qp1h1C/r/vkIIg=
|
||||||
|
github.com/fangdingjun/protolistener v0.0.0-20190415072226-223d4e06b774 h1:6w2gWbyt7GEOP7fKhnkkNPvgx7t5l5QIWWtVMnSY+ek=
|
||||||
|
github.com/fangdingjun/protolistener v0.0.0-20190415072226-223d4e06b774/go.mod h1:eVOT1s9kw8XsWs6GSG7cGuyeQGb3SqejEPD9slyHN4E=
|
||||||
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/pires/go-proxyproto v0.0.0-20190111085350-4d51b51e3bfc h1:lNOt1SMsgHXTdpuGw+RpnJtzUcCb/oRKZP65pBy9pr8=
|
||||||
|
github.com/pires/go-proxyproto v0.0.0-20190111085350-4d51b51e3bfc/go.mod h1:6/gX3+E/IYGa0wMORlSMla999awQFdbaeQCHjSMKIzY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a h1:Igim7XhdOpBnWPuYJ70XcNpq8q3BCACtVgNfoJxOV7g=
|
||||||
|
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e h1:nFYrTHrdrAOpShe27kaFHjsqYSEQ0KWqdWLu3xuZJts=
|
||||||
|
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
@ -0,0 +1,103 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/fangdingjun/go-log"
|
||||||
|
"github.com/fangdingjun/protolistener"
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProxyProto(t *testing.T) {
|
||||||
|
log.Default.Level = log.DEBUG
|
||||||
|
|
||||||
|
data, err := ioutil.ReadFile("config.sample.yaml")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if err = yaml.Unmarshal(data, &cfg); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
listener, err := net.Listen("tcp", "0.0.0.0:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
log.Printf("listen %s", listener.Addr().String())
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorln(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go serve(conn)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
listener2, err := net.Listen("tcp", "0.0.0.0:8443")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer listener2.Close()
|
||||||
|
|
||||||
|
listener2 = tls.NewListener(protolistener.New(listener2), &tls.Config{
|
||||||
|
Certificates: []tls.Certificate{cert},
|
||||||
|
})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
conn, err := listener2.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func(c net.Conn) {
|
||||||
|
defer c.Close()
|
||||||
|
addr := conn.RemoteAddr()
|
||||||
|
_conn := c.(*tls.Conn)
|
||||||
|
if err := _conn.Handshake(); err != nil {
|
||||||
|
log.Errorf("handshake error: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("%+v", _conn.ConnectionState())
|
||||||
|
log.Debugf("addr: %s", addr.String())
|
||||||
|
//fmt.Fprintf(conn, "%s", addr.String())
|
||||||
|
//conn.Close()
|
||||||
|
conn.Write([]byte(addr.String()))
|
||||||
|
// time.Sleep(1 * time.Second)
|
||||||
|
log.Debugln("reply addr")
|
||||||
|
// conn.Close()
|
||||||
|
}(conn)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
//time.Sleep(1 * time.Second)
|
||||||
|
conn, err := tls.Dial("tcp", listener.Addr().String(), &tls.Config{
|
||||||
|
ServerName: "www.example.com",
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("dial error")
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
conn.Handshake()
|
||||||
|
buf := make([]byte, 200)
|
||||||
|
n, err := conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("read error")
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
addr1 := conn.LocalAddr().String()
|
||||||
|
addr2 := string(buf[:n])
|
||||||
|
conn.Close()
|
||||||
|
if addr1 != addr2 {
|
||||||
|
t.Errorf("expect %s, got: %s", addr1, addr2)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue