]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/client/proxy.go
Refactor govpn-client.
[govpn.git] / src / cypherpunks.ru / govpn / client / proxy.go
similarity index 65%
rename from src/cypherpunks.ru/govpn/cmd/govpn-client/proxy.go
rename to src/cypherpunks.ru/govpn/client/proxy.go
index 0a6729c1dd7ca99ae0ce8f66bc4793ace0bd8a3e..9f8e684868b073c02af62ad63fa1b4608a731728 100644 (file)
@@ -16,32 +16,34 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-package main
+package client
 
 import (
        "bufio"
        "encoding/base64"
-       "log"
        "net"
+       "fmt"
        "net/http"
 
        "cypherpunks.ru/govpn"
 )
 
-func proxyTCP(timeouted, rehandshaking, termination chan struct{}) {
-       proxyAddr, err := net.ResolveTCPAddr("tcp", *proxyAddr)
+func (c *Client) proxyTCP() {
+       proxyAddr, err := net.ResolveTCPAddr("tcp", c.config.ProxyAddress)
        if err != nil {
-               log.Fatalln("Can not resolve proxy address:", err)
+               c.Error <- err
+               return
        }
        conn, err := net.DialTCP("tcp", nil, proxyAddr)
        if err != nil {
-               log.Fatalln("Can not connect to proxy:", err)
+               c.Error <- err
+               return
        }
-       req := "CONNECT " + *remoteAddr + " HTTP/1.1\n"
-       req += "Host: " + *remoteAddr + "\n"
-       if *proxyAuth != "" {
+       req := "CONNECT " + c.config.ProxyAddress + " HTTP/1.1\n"
+       req += "Host: " + c.config.ProxyAddress + "\n"
+       if c.config.ProxyAuthentication != "" {
                req += "Proxy-Authorization: Basic "
-               req += base64.StdEncoding.EncodeToString([]byte(*proxyAuth)) + "\n"
+               req += base64.StdEncoding.EncodeToString([]byte(c.config.ProxyAuthentication)) + "\n"
        }
        req += "\n"
        conn.Write([]byte(req))
@@ -50,8 +52,9 @@ func proxyTCP(timeouted, rehandshaking, termination chan struct{}) {
                &http.Request{Method: "CONNECT"},
        )
        if err != nil || resp.StatusCode != http.StatusOK {
-               log.Fatalln("Unexpected response from proxy")
+               c.Error <- fmt.Errorf("Unexpected response from proxy: %s", err.Error())
+               return
        }
-       govpn.Printf(`[proxy-connected remote="%s" addr="%s"]`, *remoteAddr, *proxyAddr)
-       go handleTCP(conn, timeouted, rehandshaking, termination)
+       govpn.Printf(`[proxy-connected remote="%s" addr="%s"]`, c.config.RemoteAddress, *proxyAddr)
+       go c.handleTCP(conn)
 }