2017-01-27 14:49:58 -06:00
|
|
|
/*
|
|
|
|
Copyright 2016-2017 gtalent2@gmail.com
|
|
|
|
|
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
2017-01-27 15:28:16 -06:00
|
|
|
"time"
|
2017-01-27 14:49:58 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CONN_ACTIVE = iota
|
|
|
|
CONN_DISCONNECTED
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConnStatus struct {
|
|
|
|
Status int
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func portForward(wanConn *net.TCPConn, lanIp, port string, connStatus chan ConnStatus) {
|
|
|
|
done := make(chan error)
|
|
|
|
log.Print("Proxy: Connecting to ", lanIp+":"+port)
|
|
|
|
lanConn, err := net.Dial("tcp", lanIp+":"+port)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Proxy: LAN dial error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go forwardConn(wanConn, lanConn, done)
|
|
|
|
go forwardConn(lanConn, wanConn, done)
|
|
|
|
|
2017-01-27 15:28:16 -06:00
|
|
|
ticker := time.NewTicker(time.Minute * 1)
|
2017-01-27 14:49:58 -06:00
|
|
|
for i := 0; i < 2; i++ {
|
2017-01-27 15:28:16 -06:00
|
|
|
select {
|
|
|
|
case err = <-done:
|
|
|
|
if err != nil {
|
|
|
|
log.Print("Proxy:", err)
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
|
|
|
connStatus <- ConnStatus{Status: CONN_ACTIVE}
|
2017-01-27 14:49:58 -06:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 15:28:16 -06:00
|
|
|
ticker.Stop()
|
2017-01-27 14:49:58 -06:00
|
|
|
|
|
|
|
wanConn.Close()
|
|
|
|
lanConn.Close()
|
|
|
|
|
|
|
|
connStatus <- ConnStatus{Status: CONN_DISCONNECTED, Err: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
func forwardConn(writer, reader net.Conn, done chan error) {
|
|
|
|
_, err := io.Copy(writer, reader)
|
|
|
|
done <- err
|
|
|
|
}
|