Fix nix builds: go dependencies are awful
This commit is contained in:
27
netforward/buffer.go
Normal file
27
netforward/buffer.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// From https://github.com/goburrow/netforward
|
||||
package netforward
|
||||
|
||||
import "sync"
|
||||
|
||||
const (
|
||||
bufferSize = 32 * 1024
|
||||
)
|
||||
|
||||
var (
|
||||
bufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, bufferSize)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func getBuffer() []byte {
|
||||
return bufferPool.Get().([]byte)
|
||||
}
|
||||
|
||||
func releaseBuffer(b []byte) {
|
||||
if len(b) != bufferSize {
|
||||
panic("attempted to release buffer with invalid length")
|
||||
}
|
||||
bufferPool.Put(b)
|
||||
}
|
||||
20
netforward/netforward.go
Normal file
20
netforward/netforward.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// From https://github.com/goburrow/netforward
|
||||
package netforward
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
// Dialer dials to a remote address.
|
||||
type Dialer interface {
|
||||
Dial() (net.Conn, error)
|
||||
}
|
||||
|
||||
func IsPacketNetwork(network string) bool {
|
||||
switch network {
|
||||
case "udp", "udp4", "udp6", "ip", "ip4", "ip6", "unixgram":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
102
netforward/packet.go
Normal file
102
netforward/packet.go
Normal file
@@ -0,0 +1,102 @@
|
||||
// From https://github.com/goburrow/netforward
|
||||
package netforward
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type packetConn struct {
|
||||
conn net.PacketConn
|
||||
addr net.Addr
|
||||
}
|
||||
|
||||
func (p *packetConn) Write(b []byte) (int, error) {
|
||||
return p.conn.WriteTo(b, p.addr)
|
||||
}
|
||||
|
||||
type syncConns struct {
|
||||
mu sync.RWMutex
|
||||
conns map[string]net.Conn
|
||||
}
|
||||
|
||||
func (f *syncConns) get(addr net.Addr) (net.Conn, bool) {
|
||||
var key string
|
||||
if addr != nil {
|
||||
key = addr.String()
|
||||
}
|
||||
f.mu.RLock()
|
||||
defer f.mu.RUnlock()
|
||||
conn, ok := f.conns[key]
|
||||
return conn, ok
|
||||
}
|
||||
|
||||
func (f *syncConns) set(addr net.Addr, conn net.Conn) {
|
||||
var key string
|
||||
if addr != nil {
|
||||
key = addr.String()
|
||||
}
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.conns[key] = conn
|
||||
}
|
||||
|
||||
func (f *syncConns) remove(addr net.Addr) {
|
||||
var key string
|
||||
if addr != nil {
|
||||
key = addr.String()
|
||||
}
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
delete(f.conns, key)
|
||||
}
|
||||
|
||||
func ForwardPacket(remote Dialer, local net.PacketConn) error {
|
||||
conns := syncConns{
|
||||
conns: make(map[string]net.Conn),
|
||||
}
|
||||
buf := getBuffer()
|
||||
defer releaseBuffer(buf)
|
||||
for {
|
||||
nr, addr, err := local.ReadFrom(buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
remoteConn, ok := conns.get(addr)
|
||||
if !ok {
|
||||
remoteConn, err = remote.Dial()
|
||||
if err != nil {
|
||||
log.Printf("%s: dial failed: %v", addr, err)
|
||||
continue
|
||||
}
|
||||
conns.set(addr, remoteConn)
|
||||
// unix datagram does not have a network addr
|
||||
if addr != nil {
|
||||
go forwardPacket(remoteConn, local, addr, &conns)
|
||||
}
|
||||
}
|
||||
nw, err := remoteConn.Write(buf[:nr])
|
||||
if err != nil {
|
||||
log.Printf("%s: write failed: %v", addr, err)
|
||||
continue
|
||||
}
|
||||
if nr != nw {
|
||||
log.Printf("%s: %v", addr, io.ErrShortWrite)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func forwardPacket(remote net.Conn, local net.PacketConn, addr net.Addr, conns *syncConns) {
|
||||
defer remote.Close()
|
||||
defer conns.remove(addr)
|
||||
|
||||
buf := getBuffer()
|
||||
defer releaseBuffer(buf)
|
||||
|
||||
_, err := io.CopyBuffer(&packetConn{local, addr}, remote, buf)
|
||||
if err != nil {
|
||||
log.Printf("%s: %v", addr, err)
|
||||
}
|
||||
}
|
||||
47
netforward/stream.go
Normal file
47
netforward/stream.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// From https://github.com/goburrow/netforward
|
||||
package netforward
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func Forward(remote Dialer, local net.Listener) error {
|
||||
for {
|
||||
conn, err := local.Accept()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go forward(remote, conn)
|
||||
}
|
||||
}
|
||||
|
||||
func forward(remote Dialer, conn io.ReadWriteCloser) {
|
||||
defer conn.Close()
|
||||
|
||||
remoteConn, err := remote.Dial()
|
||||
if err != nil {
|
||||
log.Printf("dial failed: %v", err)
|
||||
return
|
||||
}
|
||||
defer remoteConn.Close()
|
||||
|
||||
// remote -> local
|
||||
go func() {
|
||||
buf := getBuffer()
|
||||
defer releaseBuffer(buf)
|
||||
_, err := io.CopyBuffer(remoteConn, conn, buf)
|
||||
if err != nil {
|
||||
log.Printf("forward failed: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// local -> remote
|
||||
buf := getBuffer()
|
||||
defer releaseBuffer(buf)
|
||||
_, err = io.CopyBuffer(conn, remoteConn, buf)
|
||||
if err != nil {
|
||||
log.Printf("forward failed: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user