Example #1
0
func TestListenHTTP(t *testing.T) {
	px = proxy.NewProxy()

	go func(t *testing.T) {
		if err := px.Start(listenHTTPAddr); err != nil {
			t.Fatal(err)
		}
	}(t)

	time.Sleep(time.Millisecond * 100)
}
Example #2
0
func main() {
	// Banner.
	log.Printf("Hyperfox v%s // https://hyperfox.org\n", version)
	log.Printf("By José Carlos Nieto.\n\n")

	// Parsing command line flags.
	flag.Parse()

	// Opening database.
	var err error
	sess, err = dbInit()
	if err != nil {
		log.Fatal("Failed to setup database: ", err)
	}
	defer sess.Close()

	storage = sess.Collection(defaultCaptureCollection)
	if !storage.Exists() {
		log.Fatal("Storage table does not exist")
	}

	// Is SSL enabled?
	var sslEnabled bool
	if *flagSSLPort > 0 && *flagSSLCertFile != "" {
		sslEnabled = true
	}

	// User requested SSL mode.
	if sslEnabled {
		if *flagSSLCertFile == "" {
			flag.Usage()
			log.Fatal("Missing root CA certificate")
		}

		if *flagSSLKeyFile == "" {
			flag.Usage()
			log.Fatal("Missing root CA private key")
		}

		os.Setenv(proxy.EnvSSLCert, *flagSSLCertFile)
		os.Setenv(proxy.EnvSSLKey, *flagSSLKeyFile)
	}

	// Creating proxy.
	p := proxy.NewProxy()

	// Attaching logger.
	p.AddLogger(logger.Stdout{})

	// Attaching capture tool.
	res := make(chan capture.Response, 256)

	p.AddBodyWriteCloser(capture.New(res))

	// Saving captured data with a goroutine.
	go func() {
		for {
			select {
			case r := <-res:
				go func() {
					if _, err := storage.Insert(r); err != nil {
						log.Printf("Failed to save to database: %s", err)
					}
				}()
			}
		}
	}()

	if err = startServices(); err != nil {
		log.Fatal("startServices:", err)
	}

	fmt.Println("")

	var wg sync.WaitGroup

	// Starting proxy servers.
	if *flagPort > 0 {
		wg.Add(1)
		go func() {
			defer wg.Done()
			if err := p.Start(fmt.Sprintf("%s:%d", *flagAddress, *flagPort)); err != nil {
				log.Fatalf("Failed to bind on the given interface (HTTP): ", err)
			}
		}()
	}

	if sslEnabled {
		wg.Add(1)
		go func() {
			defer wg.Done()
			if err := p.StartTLS(fmt.Sprintf("%s:%d", *flagAddress, *flagSSLPort)); err != nil {
				log.Fatalf("Failed to bind on the given interface (HTTPS): ", err)
			}
		}()
	}

	if *flagUnixSocket != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			if err := p.StartUnix(proxyUnixSocket, *flagUnixSocket); err != nil {
				log.Fatalf("Failed to bind on %s: %s", proxyUnixSocket, err)
			}
		}()
	}

	wg.Wait()
}