Commits

desmaj committed 56b72ae

Added support for config files

Comments (0)

Files changed (11)

examples/echo.cfg

+{"StaticDirectory": "/Users/desmaj/Source/aquchinchay/src/aquchinchay/static",
+ "Servers":[{"ListenerAddress": "0.0.0.0:9999",
+             "ClientConfig": {"Name": "echo-server",
+                             "Client": "echo"
+                              }
+	    }
+           ]
+}

examples/multi.cfg

+{"StaticDirectory": "/Users/desmaj/Source/aquchinchay/src/aquchinchay/static",
+ "Servers":[{"ListenerAddress": "0.0.0.0:9999",
+             "ClientConfig": {"Name": "echo-server",
+                              "Client": "echo"
+                              }
+	    },
+            {"ListenerAddress": "0.0.0.0:9998",
+             "ClientConfig": {"Name": "proxy-server",
+                              "Client": "proxy",
+                              "RemoteAddress": "\\w+\\.freenode\\.net\\:6667"
+                              }
+	    }
+           ]
+}

src/aquchinchay/clients.go

+package aquchinchay
+
+func getClientFactory(factoryName string) func (ClientConfig) RemoteClient {
+	return map[string]func (ClientConfig) RemoteClient{
+		"echo": OpenEcho,
+		"proxy": OpenProxy}[factoryName]
+}
+
+func MakeClient(clientConfig ClientConfig) func () RemoteClient {
+	clientFactory := getClientFactory(clientConfig["Client"])
+	return func () RemoteClient {
+		client := clientFactory(clientConfig)
+		return client
+	}
+}

src/aquchinchay/config.go

+package aquchinchay
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+)
+
+type ClientConfig map[string]string
+
+type ServiceConfig struct {
+	ListenerAddress string
+	ClientConfig ClientConfig
+}
+
+type GlobalConfig struct {
+	Servers []ServiceConfig
+	StaticDirectory string
+}
+
+func LoadConfig (configPath string) GlobalConfig {
+	configJSON, err := ioutil.ReadFile(configPath)
+	if err != nil {
+		panic(err)
+	}
+	var rawConfig GlobalConfig
+	err = json.Unmarshal(configJSON, &rawConfig)
+	if err != nil {
+		panic(err)
+	}
+	return rawConfig
+}

src/aquchinchay/csp/server.go

 	}
 }
 
-func (server *CSPServer) start (address string) {
-	http.HandleFunc("/csp/handshake", handshake(server))
-	http.HandleFunc("/csp/send", send(server))
-	http.HandleFunc("/csp/comet", comet(server))
-	http.Handle("/csp/static/", 
+func NewServer (
+	globalConfig aquchinchay.GlobalConfig,
+	serverConfig aquchinchay.ServiceConfig) CSPServer {
+	server := CSPServer{
+		make(map[string]CSPSession), 
+		aquchinchay.MakeClient(serverConfig.ClientConfig),
+		globalConfig.StaticDirectory}
+	return server
+}
+
+func (server *CSPServer) start (address string, resultChan chan error) {
+	handler := http.NewServeMux()
+	handler.HandleFunc("/csp/handshake", handshake(server))
+	handler.HandleFunc("/csp/send", send(server))
+	handler.HandleFunc("/csp/comet", comet(server))
+	handler.Handle("/csp/static/", 
 		http.StripPrefix("/csp/static/",
 		http.FileServer(http.Dir(server.staticDirectory))))
-	http.ListenAndServe(address, nil)
+	go func () {
+		err := http.ListenAndServe(address, handler)
+		resultChan <- err
+	}()
 }
 
-func StartServer(address string, clientFactory func () aquchinchay.RemoteClient) {
-	server := CSPServer{
-		make(map[string]CSPSession), 
-		clientFactory,
-		"/home/desmaj/Source/aquchinchay/src/aquchinchay/static",
-	}
-	server.start(address)	
+func StartServer (
+	globalConfig aquchinchay.GlobalConfig,
+	serverConfig aquchinchay.ServiceConfig,
+	resultChan chan error) {
+	server := NewServer(globalConfig, serverConfig)
+	server.start(serverConfig.ListenerAddress, resultChan)
 }

src/aquchinchay/echo.go

 	channel chan string
 }
 
-func OpenEcho () RemoteClient {
+func OpenEcho (clientConfig ClientConfig) RemoteClient {
 	client := EchoClient{make(chan string, 1024)}
 	return client
 }

src/aquchinchay/proxy.go

 import (
 	"encoding/base64"
 	"fmt"
+	"log"
 	"net"
+	"regexp"
 	"strings"
 )
 
 	channel chan string
 	Conn net.Conn
 	ProxyState int
+	RemoteAddress string
 }
 
-func OpenProxy () RemoteClient {
-	return &TCPProxyClient{make(chan string, 1024), nil, PROXYINITIAL}
+func OpenProxy (clientConfig ClientConfig) RemoteClient {
+	return &TCPProxyClient{
+		make(chan string, 1024), 
+		nil, 
+		PROXYINITIAL,
+		clientConfig["RemoteAddress"]}
 }
 
 func (client *TCPProxyClient) Send (message string) {
 	switch command {
 	case "open":
 		address := messageParts[1]
-		conn, err := net.Dial("tcp", address)
-		fmt.Println(err)
-		client.Conn = conn
-		client.channel <- "OK"
-		go func () {
-			bytes := make([]byte, 1024)
-			for {
-				byteCount, err := conn.Read(bytes)
-				if err != nil {
-					client.channel <- "close 500"
-					break
-				} else {
-					client.channel <- ("data " + base64.URLEncoding.EncodeToString(bytes[:byteCount]))
+		if client.checkAddress(address) {
+			conn, err := net.Dial("tcp", address)
+			fmt.Println(err)
+			client.Conn = conn
+			client.channel <- "OK"
+			go func () {
+				bytes := make([]byte, 1024)
+				for {
+					byteCount, err := conn.Read(bytes)
+					if err != nil {
+						client.channel <- "close 500"
+						break
+					} else {
+						client.channel <- ("data " + base64.URLEncoding.EncodeToString(bytes[:byteCount]))
+					}
+					
 				}
-						
-			}
-		}()
+			}()
+		} else {
+			log.Println("Illegal remote connection: " + address)
+		}
 	case "send":
 		payload, _ := base64.URLEncoding.DecodeString(messageParts[1])
 		fmt.Println(payload)
 	}
 }
 
+func (client TCPProxyClient) checkAddress (address string) bool {
+	legal, _ := regexp.MatchString(client.RemoteAddress, address)
+	return legal
+}
+
 func (client TCPProxyClient) Channel () chan string {
 	return client.channel
 }

src/aquchinchay/static/client.js

     self.onclose = function (code) {};
     
     self.connect = function (url) {
+	console.log(url)
 	self.url = url;
 	self.readyState = READYSTATE_OPENING;
 	

src/aquchinchay/static/csp_test.js

 	div.html(data);
 	$("#console").append(div);
     };
-    client.connect('http://localhost:9998/csp')
+    client.connect('http://' + window.location.host + '/csp')
 });
     

src/aquchinchay/static/proxy.js

 		}
 	    }
 	}
-	self._session.connect('http://localhost:9999/csp')
+	self._session.connect('http://' + window.location.host + '/csp')
     }
     
     self.send = function (data) {
 import (
 	"aquchinchay"
 	"aquchinchay/csp"
+	"flag"
+	"fmt"
 )
 
 func main() {
-	// csp.StartServer("0.0.0.0:9998", aquchinchay.OpenEcho)
-	csp.StartServer("0.0.0.0:9999", aquchinchay.OpenProxy)
+	flag.Parse()
+	configPath := "aquchinchay.cfg"
+	args := flag.Args()
+	fmt.Println(args)
+	if len(args) > 0 {
+		configPath = args[0]
+	}
+	config := aquchinchay.LoadConfig(configPath)
+	resultChan := make(chan error, len(config.Servers))
+	for _, serviceConfig := range(config.Servers) {
+		csp.StartServer(config, serviceConfig, resultChan)
+	}
+	err := <- resultChan
+ 	fmt.Println(err)
 }