This commit is contained in:
esrrhs 2018-12-16 19:32:23 +08:00
parent 6b88744d14
commit eb1fe01a48
2 changed files with 52 additions and 18 deletions

View File

@ -51,7 +51,7 @@ func main() {
fmt.Printf("ERROR: %s\n", err.Error()) fmt.Printf("ERROR: %s\n", err.Error())
return return
} }
fmt.Printf("Client Listen %s (%s) Target %s (%s):\n", c.Addr(), c.IPAddr(), c.TargetAddr(), c.TargetIPAddr())
c.Run() c.Run()
fmt.Printf("Client Listen %s (%s) Target %s (%s):\n", c.Addr(), c.IPAddr(), c.TargetAddr(), c.TargetIPAddr())
} }
} }

View File

@ -54,10 +54,11 @@ type PingTunnelClient struct {
ipaddr *net.TCPAddr ipaddr *net.TCPAddr
addr string addr string
ipaddrTarget *net.TCPAddr ipaddrTarget *net.IPAddr
addrTarget string addrTarget string
conn net.Conn conn *icmp.PacketConn
listenConn *net.TCPListener
} }
type PingTunnelServer struct { type PingTunnelServer struct {
@ -98,14 +99,14 @@ func (p *PingTunnelClient) TargetAddr() string {
return p.addrTarget return p.addrTarget
} }
func (p *PingTunnelClient) TargetIPAddr() *net.TCPAddr { func (p *PingTunnelClient) TargetIPAddr() *net.IPAddr {
return p.ipaddrTarget return p.ipaddrTarget
} }
func (p *PingTunnelServer) Run() { func (p *PingTunnelServer) Run() {
conn, err := net.ListenPacket("udp4", p.addr) conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("Error listening for ICMP packets: %s\n", err.Error())
return return
} }
@ -116,25 +117,56 @@ func (p *PingTunnelServer) Run() {
func (p *PingTunnelClient) Run() { func (p *PingTunnelClient) Run() {
conn, err := net.Dial("udp4", p.addrTarget) conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("Error listening for ICMP packets: %s\n", err.Error())
return return
} }
p.conn = conn p.conn = conn
n := 0 ipaddrTarget, err := net.ResolveIPAddr("ip", p.addrTarget)
if err != nil {
return
}
p.ipaddrTarget = ipaddrTarget
ipAddr, err := net.ResolveTCPAddr("tcp", p.addr)
if err != nil {
fmt.Printf("Error listening for Local packets: %s\n", err.Error())
return
}
p.ipaddr = ipAddr
listener, err := net.ListenTCP("tcp", p.ipaddr)
p.listenConn = listener
go p.Accept()
}
func (p *PingTunnelClient) Accept() error {
for { for {
p.Send(n, []byte("haha")) localConn, err := p.listenConn.AcceptTCP()
n++ if err != nil {
fmt.Println(err)
continue
}
localConn.SetLinger(0)
go p.handleConn(*localConn)
} }
} }
func (p *PingTunnelClient) Send(id int, data []byte) error { func (p *PingTunnelClient) handleConn(conn net.TCPConn) {
}
func (p *PingTunnelClient) sendICMP(connId int, msgType int, data []byte) error {
body := &Msg{ body := &Msg{
ID: id, ID: connId,
TYPE: msgType,
Data: data, Data: data,
} }
@ -203,7 +235,8 @@ func ipv4Payload(b []byte) []byte {
} }
type Msg struct { type Msg struct {
ID int // identifier ID int // identifier
TYPE int
Data []byte // data Data []byte // data
} }
@ -211,13 +244,14 @@ func (p *Msg) Len(proto int) int {
if p == nil { if p == nil {
return 0 return 0
} }
return 4 + len(p.Data) return 8 + len(p.Data)
} }
func (p *Msg) Marshal(proto int) ([]byte, error) { func (p *Msg) Marshal(proto int) ([]byte, error) {
b := make([]byte, 4+len(p.Data)) b := make([]byte, 8+len(p.Data))
binary.BigEndian.PutUint32(b, uint32(p.ID)) binary.BigEndian.PutUint32(b, uint32(p.ID))
copy(b[4:], p.Data) binary.BigEndian.PutUint32(b, uint32(p.TYPE))
copy(b[8:], p.Data)
return b, nil return b, nil
} }