@@ 110,6 110,8 @@ func main() {
var sigchan = make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
<-sigchan // wait for signal to come in
+ signal.Reset(os.Interrupt) // Let go handle further signals
+ logger.Print("stopping traffic")
cancel()
}()
@@ 181,6 181,7 @@ func (relay *Relay) newChannel(
}
relay.printf("Channel %d connected", id)
+
var connClose = func() {
if err := conn.Close(); err != nil {
relay.printf(
@@ 194,12 195,14 @@ func (relay *Relay) newChannel(
for {
select {
case <-ctx.Done():
+ relay.printf("channel %d writer done", id)
connClose()
return
- case buf, ok := <-input:
- if !ok {
+ case buf := <-input:
+ if buf == nil {
// channel's closed, the server side has closed, therefor
// we close the connection on upstream.
+ relay.printf("channel %d closed remotely", id)
connClose()
return
}
@@ 207,10 210,10 @@ func (relay *Relay) newChannel(
relay.printf("wrote %d bytes to channel %d", n, id)
if err == io.EOF {
relay.printf("channel %d closed", id)
- connClose()
// nothing else to read, we're done
return
} else if err != nil {
+ relay.printf("ERROR: writing to channel %d: %s", id, err)
connClose()
return
}
@@ 226,13 229,12 @@ func (relay *Relay) newChannel(
relay.printf("read %d bytes from channel %d", n, id)
if err == io.EOF {
relay.printf("channel %d closed", id)
- connClose()
output <- NewPacketClose(id)
return
}
if err != nil {
relay.printf(
- "error reading from channel %d: %s",
+ "ERROR: reading from channel %d: %s",
id, err.Error(),
)
connClose()
@@ 263,9 265,10 @@ func (relay *Relay) readServer(
go func() {
for {
var p = NewPacket()
- var n, err = p.ReadFrom(relay.server)
- relay.printf("read %d bytes from upstream", n)
+ var _, err = p.ReadFrom(relay.server)
+ // relay.printf("read %d bytes from upstream", n)
if err == io.EOF {
+ relay.printf("upstream closed connection")
close(output)
return
} else if err != nil {
@@ 329,11 332,12 @@ func (relay *Relay) writeServer(
case keepalivePacket:
// Forward the packet to the server as is
p.header.Ack = seq
- var n int64
- n, err = p.WriteTo(relay.server)
- relay.printf("wrote %d bytes upstream for keepalive", n)
+ // var n int64
+ _, err = p.WriteTo(relay.server)
+ // relay.printf("wrote %d bytes upstream for keepalive", n)
}
if err != nil {
+ relay.printf("error writing packet %s", err)
fail(err)
}
}
@@ 423,6 427,7 @@ func (relay *Relay) Run(ctx context.Cont
// half-close, we'll no longer receive data from the server
// side, but still need to forward what the client side
// sends us. Ignore it for now.
+ relay.printf("half-close!")
} else {
relay.printf("channel %d closed remotely", p.ConnectionId)
relay.dispatcher.Close(p.header.ConnectionId)