2022-11-11 14:44:44 +00:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type packet struct {
|
|
|
|
pc net.PacketConn
|
|
|
|
rAddr net.Addr
|
|
|
|
payload []byte
|
|
|
|
bufRef []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packet) Data() []byte {
|
|
|
|
return c.payload
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
|
|
|
|
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
|
|
|
if addr == nil {
|
|
|
|
err = errors.New("address is invalid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
packet := bytes.Join([][]byte{socks5.ParseAddrToSocksAddr(addr), b}, []byte{})
|
|
|
|
return c.pc.WriteTo(packet, c.rAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the source IP/Port of UDP Packet
|
|
|
|
func (c *packet) LocalAddr() net.Addr {
|
|
|
|
return c.rAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packet) Drop() {
|
|
|
|
pool.Put(c.bufRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packet) InAddr() net.Addr {
|
|
|
|
return c.pc.LocalAddr()
|
|
|
|
}
|
|
|
|
|
2022-11-11 14:48:44 +00:00
|
|
|
func ParseSSURL(s string) (addr, cipher, password string, err error) {
|
2022-11-11 14:44:44 +00:00
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = u.Host
|
|
|
|
if u.User != nil {
|
|
|
|
cipher = u.User.Username()
|
|
|
|
password, _ = u.User.Password()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|