Skip to content
Snippets Groups Projects
Unverified Commit 66fa9972 authored by meskio's avatar meskio :tent:
Browse files

Add error handling support

parent e4fbb2f0
Branches
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ type ShapeShifter struct { ...@@ -16,6 +16,7 @@ type ShapeShifter struct {
Target string // remote ip:port obfs4 server Target string // remote ip:port obfs4 server
SocksAddr string // -proxylistenaddr in shapeshifter-dispatcher SocksAddr string // -proxylistenaddr in shapeshifter-dispatcher
ln net.Listener ln net.Listener
errChan chan error
} }
func (ss *ShapeShifter) Open() error { func (ss *ShapeShifter) Open() error {
...@@ -40,6 +41,20 @@ func (ss *ShapeShifter) Close() error { ...@@ -40,6 +41,20 @@ func (ss *ShapeShifter) Close() error {
return nil return nil
} }
func (ss *ShapeShifter) GetErrorChannel() chan error {
if ss.errChan == nil {
ss.errChan = make(chan error, 2)
}
return ss.errChan
}
func (ss *ShapeShifter) GetLastError() error {
if ss.errChan == nil {
ss.errChan = make(chan error, 2)
}
return <-ss.errChan
}
func (ss ShapeShifter) clientAcceptLoop() error { func (ss ShapeShifter) clientAcceptLoop() error {
for { for {
conn, err := ss.ln.Accept() conn, err := ss.ln.Accept()
...@@ -47,6 +62,7 @@ func (ss ShapeShifter) clientAcceptLoop() error { ...@@ -47,6 +62,7 @@ func (ss ShapeShifter) clientAcceptLoop() error {
if e, ok := err.(net.Error); ok && !e.Temporary() { if e, ok := err.(net.Error); ok && !e.Temporary() {
return err return err
} }
ss.sendError("Error accepting connection: %v", err)
continue continue
} }
go ss.clientHandler(conn) go ss.clientHandler(conn)
...@@ -59,18 +75,18 @@ func (ss ShapeShifter) clientHandler(conn net.Conn) { ...@@ -59,18 +75,18 @@ func (ss ShapeShifter) clientHandler(conn net.Conn) {
transport := obfs4.NewObfs4Client(ss.Cert, ss.IatMode) transport := obfs4.NewObfs4Client(ss.Cert, ss.IatMode)
remote, err := transport.Dial(ss.Target) remote, err := transport.Dial(ss.Target)
if err != nil { if err != nil {
log.Printf("outgoing connection failed %s: %v", ss.Target, err) ss.sendError("outgoing connection failed %s: %v", ss.Target, err)
return return
} }
if remote == nil { if remote == nil {
log.Printf("outgoing connection failed %s", ss.Target) ss.sendError("outgoing connection failed %s", ss.Target)
return return
} }
defer remote.Close() defer remote.Close()
err = copyLoop(conn, remote) err = copyLoop(conn, remote)
if err != nil { if err != nil {
log.Printf("%s - closed connection: %v", ss.Target, err) ss.sendError("%s - closed connection: %v", ss.Target, err)
} else { } else {
log.Printf("%s - closed connection", ss.Target) log.Printf("%s - closed connection", ss.Target)
} }
...@@ -121,3 +137,14 @@ func (ss *ShapeShifter) checkOptions() error { ...@@ -121,3 +137,14 @@ func (ss *ShapeShifter) checkOptions() error {
} }
return nil return nil
} }
func (ss *ShapeShifter) sendError(format string, a ...interface{}) {
if ss.errChan == nil {
ss.errChan = make(chan error, 2)
}
select {
case ss.errChan <- fmt.Errorf(format, a...):
default:
log.Printf(format, a...)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment