Beispiel #1
0
// Startup and main client acceptance loop
func main() {
	if len(os.Args) != 3 {
		fmt.Printf("Usage: simpleproxy LISTENADDR SERVERADDR\n")
		os.Exit(1)
	}

	ln, err := util.AutoListen(os.Args[1])
	if err != nil {
		fmt.Printf("Could not listen on address: %v\n", err)
		os.Exit(1)
	}

	// Signal handling; this is pretty ghetto now, but at least we
	// can exit cleanly on an interrupt. N.B.: this currently does
	// not correctly capture SIGTERM on Linux (and possibly
	// elsewhere)--it just kills the process directly without
	// involving the signal handler.
	sigch := make(chan os.Signal)
	signal.Notify(sigch, os.Interrupt, os.Kill)
	watchSigs := func() {
		for sig := range sigch {
			fmt.Printf("Got signal %v", sig)
			if sig == os.Kill {
				os.Exit(2)
			} else if sig == os.Interrupt {
				os.Exit(0)
			}
		}
	}
	go watchSigs()

	target := os.Args[2]
	resolver := &fixedResolver{target}
	manager := femebe.NewSimpleSessionManager()
	p := &proxy{resolver, manager}

	for {
		conn, err := ln.Accept()

		if err != nil {
			fmt.Printf("Error: %v\n", err)
			continue
		}

		log.Print("accepting connection")

		go p.handleConnection(conn, target)
	}
}
// Startup and main client acceptance loop
func main() {
	installSignalHandlers()

	if len(os.Args) < 2 {
		log.Printf("Usage: cartographer LISTENADDR TARGETADDR")
		os.Exit(1)
	}

	ln, err := util.AutoListen(os.Args[1])
	if err != nil {
		log.Printf("Could not listen on address: %v", err)
		os.Exit(1)
	}

	activity := make(chan string)
	web := NewWebRelay(activity)
	go web.Relay()
	go web.listenHttp(8080)

	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Printf("Error: %v\n", err)
			continue
		}

		target := os.Args[2]
		resolver := &fixedResolver{target}
		manager := femebe.NewSimpleSessionManager()

		frontend := make(chan *core.Message)
		backend := make(chan *core.Message)

		p := &proxy{resolver, manager, frontend, backend}

		sw := NewSessionWatcher(frontend, backend, activity)
		go sw.listen()
		go p.handleConnection(conn)
	}

	log.Println("cartographer finished")
}