Files
proxy/server/fwd/core/conn.go

95 lines
1.5 KiB
Go

package core
import (
"bufio"
"fmt"
"net"
"sync"
"time"
)
type ConnMap struct {
_map sync.Map
}
func (c *ConnMap) LoadAndDelete(key string) (*Conn, bool) {
_value, ok := c._map.LoadAndDelete(key)
if !ok {
return nil, false
}
return _value.(*Conn), true
}
func (c *ConnMap) Store(key string, value *Conn) {
c._map.Store(key, value)
}
func (c *ConnMap) Range(f func(key string, value *Conn) bool) {
c._map.Range(func(key, value any) bool {
return f(key.(string), value.(*Conn))
})
}
func (c *ConnMap) Clear() {
c._map.Clear()
}
type Conn struct {
Conn net.Conn
Reader *bufio.Reader
Tag string
Protocol string
Dest *FwdAddr
Auth *AuthContext
}
func (c Conn) Read(b []byte) (n int, err error) {
return c.Reader.Read(b)
}
func (c Conn) Write(b []byte) (n int, err error) {
return c.Conn.Write(b)
}
func (c Conn) Close() error {
return c.Conn.Close()
}
func (c Conn) LocalAddr() net.Addr {
return c.Conn.LocalAddr()
}
func (c Conn) RemoteAddr() net.Addr {
return c.Conn.RemoteAddr()
}
func (c Conn) SetDeadline(t time.Time) error {
return c.Conn.SetDeadline(t)
}
func (c Conn) SetReadDeadline(t time.Time) error {
return c.Conn.SetReadDeadline(t)
}
func (c Conn) SetWriteDeadline(t time.Time) error {
return c.Conn.SetWriteDeadline(t)
}
func (c Conn) DestAddr() net.Addr {
return c.Dest
}
type FwdAddr struct {
IP net.IP
Port int
Domain string
}
func (a FwdAddr) Network() string {
return "tcp"
}
func (a FwdAddr) String() string {
return fmt.Sprintf("%s:%d", a.IP, a.Port)
}