Update exec/control usage

This commit is contained in:
世界 2022-08-16 18:19:48 +08:00
parent c165969399
commit 835ae1217b
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 38 additions and 56 deletions

View file

@ -3,35 +3,35 @@ package redir
import ( import (
"net" "net"
"net/netip" "net/netip"
"os"
"syscall" "syscall"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/control"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
) )
func GetOriginalDestination(conn net.Conn) (destination netip.AddrPort, err error) { func GetOriginalDestination(conn net.Conn) (destination netip.AddrPort, err error) {
rawConn, err := conn.(syscall.Conn).SyscallConn() syscallConn, ok := common.Cast[syscall.Conn](conn)
if err != nil { if !ok {
return return netip.AddrPort{}, os.ErrInvalid
} }
var rawFd uintptr err = control.Conn(syscallConn, func(fd uintptr) error {
err = rawConn.Control(func(fd uintptr) { const SO_ORIGINAL_DST = 80
rawFd = fd if conn.RemoteAddr().(*net.TCPAddr).IP.To4() != nil {
raw, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST)
if err != nil {
return err
}
destination = netip.AddrPortFrom(M.AddrFromIP(raw.Multiaddr[4:8]), uint16(raw.Multiaddr[2])<<8+uint16(raw.Multiaddr[3]))
} else {
raw, err := syscall.GetsockoptIPv6MTUInfo(int(fd), syscall.IPPROTO_IPV6, SO_ORIGINAL_DST)
if err != nil {
return err
}
destination = netip.AddrPortFrom(M.AddrFromIP(raw.Addr.Addr[:]), raw.Addr.Port)
}
return nil
}) })
if err != nil { return
return
}
const SO_ORIGINAL_DST = 80
if conn.RemoteAddr().(*net.TCPAddr).IP.To4() != nil {
raw, err := syscall.GetsockoptIPv6Mreq(int(rawFd), syscall.IPPROTO_IP, SO_ORIGINAL_DST)
if err != nil {
return netip.AddrPort{}, err
}
return netip.AddrPortFrom(M.AddrFromIP(raw.Multiaddr[4:8]), uint16(raw.Multiaddr[2])<<8+uint16(raw.Multiaddr[3])), nil
} else {
raw, err := syscall.GetsockoptIPv6MTUInfo(int(rawFd), syscall.IPPROTO_IPV6, SO_ORIGINAL_DST)
if err != nil {
return netip.AddrPort{}, err
}
return netip.AddrPortFrom(M.AddrFromIP(raw.Addr.Addr[:]), raw.Addr.Port), nil
}
} }

View file

@ -1,21 +0,0 @@
package settings
import (
"os"
"os/exec"
)
func runCommand(name string, args ...string) error {
command := exec.Command(name, args...)
command.Env = os.Environ()
command.Stdin = os.Stdin
command.Stdout = os.Stderr
command.Stderr = os.Stderr
return command.Run()
}
func readCommand(name string, args ...string) ([]byte, error) {
command := exec.Command(name, args...)
command.Env = os.Environ()
return command.CombinedOutput()
}

View file

@ -5,6 +5,7 @@ import (
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format" F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
@ -32,13 +33,13 @@ func (p *systemProxy) update() error {
return err return err
} }
if p.isMixed { if p.isMixed {
err = runCommand("networksetup", "-setsocksfirewallproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)) err = common.Exec("networksetup", "-setsocksfirewallproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)).Attach().Run()
} }
if err == nil { if err == nil {
err = runCommand("networksetup", "-setwebproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)) err = common.Exec("networksetup", "-setwebproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)).Attach().Run()
} }
if err == nil { if err == nil {
err = runCommand("networksetup", "-setsecurewebproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)) err = common.Exec("networksetup", "-setsecurewebproxy", interfaceDisplayName, "127.0.0.1", F.ToString(p.port)).Attach().Run()
} }
return err return err
} }
@ -49,19 +50,19 @@ func (p *systemProxy) unset() error {
return err return err
} }
if p.isMixed { if p.isMixed {
err = runCommand("networksetup", "-setsocksfirewallproxystate", interfaceDisplayName, "off") err = common.Exec("networksetup", "-setsocksfirewallproxystate", interfaceDisplayName, "off").Attach().Run()
} }
if err == nil { if err == nil {
err = runCommand("networksetup", "-setwebproxystate", interfaceDisplayName, "off") err = common.Exec("networksetup", "-setwebproxystate", interfaceDisplayName, "off").Attach().Run()
} }
if err == nil { if err == nil {
err = runCommand("networksetup", "-setsecurewebproxystate", interfaceDisplayName, "off") err = common.Exec("networksetup", "-setsecurewebproxystate", interfaceDisplayName, "off").Attach().Run()
} }
return err return err
} }
func getInterfaceDisplayName(name string) (string, error) { func getInterfaceDisplayName(name string) (string, error) {
content, err := readCommand("networksetup", "-listallhardwareports") content, err := common.Exec("networksetup", "-listallhardwareports").Read()
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -27,9 +27,9 @@ func init() {
func runAsUser(name string, args ...string) error { func runAsUser(name string, args ...string) error {
if os.Getuid() != 0 { if os.Getuid() != 0 {
return runCommand(name, args...) return common.Exec(name, args...).Attach().Run()
} else if sudoUser != "" { } else if sudoUser != "" {
return runCommand("su", "-", sudoUser, "-c", F.ToString(name, " ", strings.Join(args, " "))) return common.Exec("su", "-", sudoUser, "-c", F.ToString(name, " ", strings.Join(args, " "))).Attach().Run()
} else { } else {
return E.New("set system proxy: unable to set as root") return E.New("set system proxy: unable to set as root")
} }

2
go.mod
View file

@ -15,7 +15,7 @@ require (
github.com/logrusorgru/aurora v2.0.3+incompatible github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/lucas-clemente/quic-go v0.28.1 github.com/lucas-clemente/quic-go v0.28.1
github.com/oschwald/maxminddb-golang v1.10.0 github.com/oschwald/maxminddb-golang v1.10.0
github.com/sagernet/sing v0.0.0-20220815085149-6b313ff9efc3 github.com/sagernet/sing v0.0.0-20220816094748-fb82be7f3f08
github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae
github.com/sagernet/sing-shadowsocks v0.0.0-20220812082714-484a11603b48 github.com/sagernet/sing-shadowsocks v0.0.0-20220812082714-484a11603b48
github.com/sagernet/sing-tun v0.0.0-20220815033412-1407eae46bd7 github.com/sagernet/sing-tun v0.0.0-20220815033412-1407eae46bd7

6
go.sum
View file

@ -153,8 +153,10 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs=
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
github.com/sagernet/sing v0.0.0-20220815085149-6b313ff9efc3 h1:f9QdygPxD5wuAOnO6NpWF/Ra5bT6NPUOL3oQNulWSo8= github.com/sagernet/sing v0.0.0-20220816094310-1b9cf9a6266f h1:fNgFTFkBLi0oJZUZFLs2LbiITblUgxWgZNGoRU/SIXE=
github.com/sagernet/sing v0.0.0-20220815085149-6b313ff9efc3/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.0.0-20220816094310-1b9cf9a6266f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
github.com/sagernet/sing v0.0.0-20220816094748-fb82be7f3f08 h1:Z5UMSxFO+c2GtJqDlU7SF4OqzEV76KNYktTyzhofL9o=
github.com/sagernet/sing v0.0.0-20220816094748-fb82be7f3f08/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae h1:xOpbvgizvIbKKrrcl/CK3RjGY2u7rC+SBXlgqzEZOU4= github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae h1:xOpbvgizvIbKKrrcl/CK3RjGY2u7rC+SBXlgqzEZOU4=
github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM= github.com/sagernet/sing-dns v0.0.0-20220813025814-e656c9dbf3ae/go.mod h1:T77zZdE2Cm6VqnFumrpwsq+kxYsbq+vWDhmjtdSl/oM=
github.com/sagernet/sing-shadowsocks v0.0.0-20220812082714-484a11603b48 h1:NlcTFKldteZvYBDyr+V9MjZEI0rAWCSFCyLgPvc5n/Y= github.com/sagernet/sing-shadowsocks v0.0.0-20220812082714-484a11603b48 h1:NlcTFKldteZvYBDyr+V9MjZEI0rAWCSFCyLgPvc5n/Y=