From 4a56b1440c2bc315adda61b542793b7780cb8730 Mon Sep 17 00:00:00 2001 From: Bluesaxorcist Date: Mon, 21 Oct 2019 17:32:39 -0500 Subject: Removed termmon and fixed compiler warnings --- common/log/log.go | 16 +---- common/options.go | 8 +-- common/pt_extras/pt_extras.go | 6 -- common/socks5/auth_pt2.go | 14 ++--- common/socks5/rfc1929.go | 4 +- common/socks5/socks5.go | 24 +++---- common/termmon/termmon.go | 136 ---------------------------------------- common/termmon/termmon_linux.go | 49 --------------- 8 files changed, 27 insertions(+), 230 deletions(-) delete mode 100644 common/termmon/termmon.go delete mode 100644 common/termmon/termmon_linux.go (limited to 'common') diff --git a/common/log/log.go b/common/log/log.go index 1c30b48..bcf83ea 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -59,7 +59,7 @@ var enableLogging bool var unsafeLogging bool // Init initializes logging with the given path, and log safety options. -func Init(enable bool, logFilePath string, unsafe bool) error { +func Init(enable bool, logFilePath string) error { if enable { f, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { @@ -78,12 +78,6 @@ func Enabled() bool { return enableLogging } -// Unsafe returns if unsafe logging is allowed (the caller MAY skip eliding -// addresses and other bits of sensitive information). -func Unsafe() bool { - return unsafeLogging -} - // Level returns the current log level. func Level() int { return logLevel @@ -140,14 +134,6 @@ func Infof(format string, a ...interface{}) { } } -// Debugf logs the given format string/arguments at the DEBUG log level. -func Debugf(format string, a ...interface{}) { - if enableLogging && logLevel >= LevelDebug { - msg := fmt.Sprintf(format, a...) - log.Print("[DEBUG]: " + msg) - } -} - // ElideError transforms the string representation of the provided error // based on the unsafeLogging setting. Callers that wish to log errors // returned from Go's net package should use ElideError to sanitize the diff --git a/common/options.go b/common/options.go index f0ededf..8382544 100644 --- a/common/options.go +++ b/common/options.go @@ -40,7 +40,7 @@ func ParseServerOptions(s string) (params map[string]map[string]interface{}, err return result, nil } -func CoerceToString(futureString interface{}) (*string, error) { +func CoerceToString(futureString interface{}) (string, error) { var result string switch futureString.(type) { @@ -48,10 +48,10 @@ func CoerceToString(futureString interface{}) (*string, error) { var icerr error result, icerr = interconv.ParseString(futureString) if icerr != nil { - return nil, icerr + return "", icerr } - return &result, nil + return result, nil default: - return nil, errors.New("unable to coerce empty interface to string") + return "", errors.New("unable to coerce empty interface to string") } } \ No newline at end of file diff --git a/common/pt_extras/pt_extras.go b/common/pt_extras/pt_extras.go index a04859e..b04426f 100644 --- a/common/pt_extras/pt_extras.go +++ b/common/pt_extras/pt_extras.go @@ -169,12 +169,6 @@ func resolveAddrStr(addrStr string) (*net.TCPAddr, error) { return &net.TCPAddr{IP: ip, Port: int(port), Zone: ""}, nil } -// Feature #15435 adds a new env var for determining if Tor keeps stdin -// open for use in termination detection. -func PtShouldExitOnStdinClose() bool { - return os.Getenv("TOR_PT_EXIT_ON_STDIN_CLOSE") == "1" -} - func ArgsToDialer(target string, name string, args map[string]interface{}, dialer proxy.Dialer) (Optimizer.Transport, error) { switch name { //case "obfs2": diff --git a/common/socks5/auth_pt2.go b/common/socks5/auth_pt2.go index ce3a50d..7e22a0f 100644 --- a/common/socks5/auth_pt2.go +++ b/common/socks5/auth_pt2.go @@ -35,24 +35,24 @@ import ( func (req *Request) authPT2() (err error) { // The client sends a PT 2.0 authentication request. - // uint32_t len - // uint8_t data[len] + // uint32_t u + // uint8_t data[u] // Read the authentication data. - var len uint32 - if len, err = req.readUint32(); err != nil { + var u uint32 + if u, err = req.readUint32(); err != nil { return } - if len == 0 { + if u == 0 { err = fmt.Errorf("PT 2.0 authentication data with 0 length") return } var data []byte - if data, err = req.readBytes(int(len)); err != nil { + if data, err = req.readBytes(int(u)); err != nil { return } - var result string = string(data) + var result = string(data) // Parse the authentication data according to the PT 2.0 specification if req.Args, err = pt.ParsePT2ClientParameters(result); err != nil { diff --git a/common/socks5/rfc1929.go b/common/socks5/rfc1929.go index f8176f1..d7849df 100644 --- a/common/socks5/rfc1929.go +++ b/common/socks5/rfc1929.go @@ -39,8 +39,8 @@ func (req *Request) authRFC1929() (err error) { sendErrResp := func() { // Swallow write/flush errors, the auth failure is the relevant error. resp := []byte{authRFC1929Ver, authRFC1929Fail} - req.rw.Write(resp[:]) - req.flushBuffers() + _, _ = req.rw.Write(resp[:]) + _ = req.flushBuffers() } // The client sends a Username/Password request. diff --git a/common/socks5/socks5.go b/common/socks5/socks5.go index 74e1175..002ba7b 100644 --- a/common/socks5/socks5.go +++ b/common/socks5/socks5.go @@ -111,6 +111,8 @@ func ErrorToReplyCode(err error) ReplyCode { return ReplyHostUnreachable case syscall.ECONNREFUSED, syscall.ECONNRESET: return ReplyConnectionRefused + case syscall.EPERM: + return ReplyConnectionNotAllowed default: return ReplyGeneralFailure } @@ -267,15 +269,15 @@ func (req *Request) readCommand() error { var err error if err = req.readByteVerify("version", version); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } if err = req.readByteVerify("command", cmdConnect); err != nil { - req.Reply(ReplyCommandNotSupported) + _ = req.Reply(ReplyCommandNotSupported) return err } if err = req.readByteVerify("reserved", rsv); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } @@ -283,49 +285,49 @@ func (req *Request) readCommand() error { var atyp byte var host string if atyp, err = req.readByte(); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } switch atyp { case atypIPv4: var addr []byte if addr, err = req.readBytes(net.IPv4len); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } host = net.IPv4(addr[0], addr[1], addr[2], addr[3]).String() case atypDomainName: var alen byte if alen, err = req.readByte(); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } if alen == 0 { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return fmt.Errorf("domain name with 0 length") } var addr []byte if addr, err = req.readBytes(int(alen)); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } host = string(addr) case atypIPv6: var rawAddr []byte if rawAddr, err = req.readBytes(net.IPv6len); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } addr := make(net.IP, net.IPv6len) copy(addr[:], rawAddr[:]) host = fmt.Sprintf("[%s]", addr.String()) default: - req.Reply(ReplyAddressNotSupported) + _ = req.Reply(ReplyAddressNotSupported) return fmt.Errorf("unsupported address type 0x%02x", atyp) } var rawPort []byte if rawPort, err = req.readBytes(2); err != nil { - req.Reply(ReplyGeneralFailure) + _ = req.Reply(ReplyGeneralFailure) return err } port := int(rawPort[0])<<8 | int(rawPort[1]) diff --git a/common/termmon/termmon.go b/common/termmon/termmon.go deleted file mode 100644 index 716bef6..0000000 --- a/common/termmon/termmon.go +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (c) 2015, Yawning Angel - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -package termmon - -import ( - "io" - "io/ioutil" - "os" - "os/signal" - "runtime" - "syscall" - "time" - - "github.com/OperatorFoundation/shapeshifter-dispatcher/common/log" - "github.com/OperatorFoundation/shapeshifter-dispatcher/common/pt_extras" -) - -var TermMonitorOSInit func(*TermMonitor) error - -type TermMonitor struct { - sigChan chan os.Signal - handlerChan chan int - numHandlers int -} - -func (m *TermMonitor) OnHandlerStart() { - m.handlerChan <- 1 -} - -func (m *TermMonitor) OnHandlerFinish() { - m.handlerChan <- -1 -} - -func (m *TermMonitor) Wait(termOnNoHandlers bool) os.Signal { - // Block until a signal has been received, or (optionally) the - // number of pending handlers has hit 0. In the case of the - // latter, treat it as if a SIGTERM has been received. - for { - select { - case n := <-m.handlerChan: - m.numHandlers += n - case sig := <-m.sigChan: - return sig - } - if termOnNoHandlers && m.numHandlers == 0 { - return syscall.SIGTERM - } - } -} - -func (m *TermMonitor) termOnStdinClose() { - _, err := io.Copy(ioutil.Discard, os.Stdin) - - // io.Copy() will return a nil on EOF, since reaching EOF is - // expected behavior. No matter what, if this unblocks, assume - // that stdin is closed, and treat that as having received a - // SIGTERM. - log.Noticef("Stdin is closed or unreadable: %v", err) - m.sigChan <- syscall.SIGTERM -} - -func (m *TermMonitor) termOnPPIDChange(ppid int) { - // Under most if not all U*IX systems, the parent PID will change - // to that of init once the parent dies. There are several notable - // exceptions (Slowlaris/Android), but the parent PID changes - // under those platforms as well. - // - // Naturally we lose if the parent has died by the time when the - // Getppid() call was issued in our parent, but, this is better - // than nothing. - const ppidPollInterval = 1 * time.Second - for ppid == os.Getppid() { - time.Sleep(ppidPollInterval) - } - - // Treat the parent PID changing as the same as having received - // a SIGTERM. - log.Noticef("Parent pid changed: %d (was %d)", os.Getppid(), ppid) - m.sigChan <- syscall.SIGTERM -} - -func NewTermMonitor(exitOnStdinClose bool) (m *TermMonitor) { - ppid := os.Getppid() - m = new(TermMonitor) - m.sigChan = make(chan os.Signal) - m.handlerChan = make(chan int) - signal.Notify(m.sigChan, syscall.SIGINT, syscall.SIGTERM) - - // If tor supports feature #15435, we can use Stdin being closed as an - // indication that tor has died, or wants the PT to shutdown for any - // reason. - if exitOnStdinClose || pt_extras.PtShouldExitOnStdinClose() { - go m.termOnStdinClose() - } else { - // Instead of feature #15435, use various kludges and hacks: - // * Linux - Platform specific code that should always work. - // * Other U*IX - Somewhat generic code, that works unless the - // parent dies before the monitor is initialized. - if TermMonitorOSInit != nil { - // Errors here are non-fatal, since it might still be - // possible to fall back to a generic implementation. - if err := TermMonitorOSInit(m); err == nil { - return - } - } - if runtime.GOOS != "windows" { - go m.termOnPPIDChange(ppid) - } - } - return -} diff --git a/common/termmon/termmon_linux.go b/common/termmon/termmon_linux.go deleted file mode 100644 index 89d3e9f..0000000 --- a/common/termmon/termmon_linux.go +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2015, Yawning Angel - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -package termmon - -import ( - "fmt" - "syscall" -) - -func TermMonitorInitLinux(m *TermMonitor) error { - // Use prctl() to have the kernel deliver a SIGTERM if the parent - // process dies. This beats anything else that can be done before - // #15435 is implemented. - _, _, errno := syscall.Syscall(syscall.SYS_PRCTL, syscall.PR_SET_PDEATHSIG, uintptr(syscall.SIGTERM), 0) - if errno != 0 { - var err error = errno - return fmt.Errorf("prctl(PR_SET_PDEATHSIG, SIGTERM) returned: %s", err) - } - return nil -} - -func init() { - TermMonitorOSInit = TermMonitorInitLinux -} -- cgit v1.2.3