add icmp tunnel
This commit is contained in:
@ -69,6 +69,7 @@ func LoggerServerOption(logger logger.Logger) ServerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: remove stale clients from conns
|
||||
type Server struct {
|
||||
addr net.Addr
|
||||
httpServer *http.Server
|
||||
|
@ -10,26 +10,19 @@ import (
|
||||
)
|
||||
|
||||
type cipherConn struct {
|
||||
*net.UDPConn
|
||||
net.PacketConn
|
||||
key []byte
|
||||
}
|
||||
|
||||
func CipherConn(conn *net.UDPConn, key []byte) net.Conn {
|
||||
func CipherPacketConn(conn net.PacketConn, key []byte) net.PacketConn {
|
||||
return &cipherConn{
|
||||
UDPConn: conn,
|
||||
key: key,
|
||||
}
|
||||
}
|
||||
|
||||
func CipherPacketConn(conn *net.UDPConn, key []byte) net.PacketConn {
|
||||
return &cipherConn{
|
||||
UDPConn: conn,
|
||||
key: key,
|
||||
PacketConn: conn,
|
||||
key: key,
|
||||
}
|
||||
}
|
||||
|
||||
func (conn *cipherConn) ReadFrom(data []byte) (n int, addr net.Addr, err error) {
|
||||
n, addr, err = conn.UDPConn.ReadFrom(data)
|
||||
n, addr, err = conn.PacketConn.ReadFrom(data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -49,7 +42,7 @@ func (conn *cipherConn) WriteTo(data []byte, addr net.Addr) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = conn.UDPConn.WriteTo(b, addr)
|
||||
_, err = conn.PacketConn.WriteTo(b, addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -2,17 +2,25 @@ package ws
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type websocketConn struct {
|
||||
*websocket.Conn
|
||||
rb []byte
|
||||
type WebsocketConn interface {
|
||||
net.Conn
|
||||
WriteMessage(int, []byte) error
|
||||
ReadMessage() (int, []byte, error)
|
||||
}
|
||||
|
||||
func Conn(conn *websocket.Conn) net.Conn {
|
||||
type websocketConn struct {
|
||||
*websocket.Conn
|
||||
rb []byte
|
||||
mux sync.Mutex
|
||||
}
|
||||
|
||||
func Conn(conn *websocket.Conn) WebsocketConn {
|
||||
return &websocketConn{
|
||||
Conn: conn,
|
||||
}
|
||||
@ -20,7 +28,7 @@ func Conn(conn *websocket.Conn) net.Conn {
|
||||
|
||||
func (c *websocketConn) Read(b []byte) (n int, err error) {
|
||||
if len(c.rb) == 0 {
|
||||
_, c.rb, err = c.ReadMessage()
|
||||
_, c.rb, err = c.Conn.ReadMessage()
|
||||
}
|
||||
n = copy(b, c.rb)
|
||||
c.rb = c.rb[n:]
|
||||
@ -33,6 +41,13 @@ func (c *websocketConn) Write(b []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *websocketConn) WriteMessage(messageType int, data []byte) error {
|
||||
c.mux.Lock()
|
||||
defer c.mux.Unlock()
|
||||
|
||||
return c.Conn.WriteMessage(messageType, data)
|
||||
}
|
||||
|
||||
func (c *websocketConn) SetDeadline(t time.Time) error {
|
||||
if err := c.SetReadDeadline(t); err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user