mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 01:15:41 +08:00
proxy: improve interface binding (by setsockopt) on linux
This commit is contained in:
parent
5b1a127d04
commit
39ae201afe
14
proxy/bind_linux.go
Normal file
14
proxy/bind_linux.go
Normal file
@ -0,0 +1,14 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func bind(dialer *net.Dialer, iface *net.Interface) {
|
||||
dialer.Control = func(network, address string, c syscall.RawConn) error {
|
||||
return c.Control(func(fd uintptr) {
|
||||
syscall.BindToDevice(int(fd), iface.Name)
|
||||
})
|
||||
}
|
||||
}
|
8
proxy/bind_others.go
Normal file
8
proxy/bind_others.go
Normal file
@ -0,0 +1,8 @@
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package proxy
|
||||
|
||||
import "net"
|
||||
|
||||
func bind(dialer *net.Dialer, iface *net.Interface) {}
|
@ -33,9 +33,6 @@ func NewDirect(intface string, dialTimeout, relayTimeout time.Duration) (*Direct
|
||||
return nil, errors.New(err.Error() + ": " + intface)
|
||||
}
|
||||
d.iface = iface
|
||||
if ips := d.IFaceIPs(); len(ips) > 0 {
|
||||
d.ip = ips[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,6 +85,10 @@ func (d *Direct) dial(network, addr string, localIP net.IP) (net.Conn, error) {
|
||||
}
|
||||
|
||||
dialer := &net.Dialer{LocalAddr: la, Timeout: d.dialTimeout}
|
||||
if d.iface != nil {
|
||||
bind(dialer, d.iface)
|
||||
}
|
||||
|
||||
c, err := dialer.Dial(network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -47,7 +47,7 @@ type chunkedReader struct {
|
||||
|
||||
// ChunkedReader returns a chunked reader.
|
||||
func ChunkedReader(r io.Reader, chunkSizeDecoder ChunkSizeDecoder) io.Reader {
|
||||
return &chunkedReader{Reader: r, chunkSizeDecoder: chunkSizeDecoder}
|
||||
return &chunkedReader{Reader: r, chunkSizeDecoder: chunkSizeDecoder, buf: make([]byte, chunkSizeDecoder.SizeBytes())}
|
||||
}
|
||||
|
||||
func (r *chunkedReader) Read(p []byte) (int, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user