Giter Site home page Giter Site logo

Comments (2)

 avatar commented on May 5, 2024

A very tiny proxy example can see https://github.com/netroby/gohttpproxy/blob/master/main.go

package main

import (
        "crypto/tls"
        "flag"
        "fmt"
        "log"
        "net"
        "net/http"
        "net/url"
        "os"
        "os/signal"
        "time"

        "github.com/google/martian"
        "golang.org/x/net/proxy"
        "syscall"
)

var (
        addr    = flag.String("addr", ":8080", "host:port of the proxy")
        forward = flag.String("forward", "", "forward to upstream proxy, example: socks5://127.0.0.1:1080")
)

func main() {
        p := martian.NewProxy()
        defer p.Close()

        fmt.Printf("Now will connect parent proxy: %v \n", *forward)
        if *forward != "" {
                url, err := url.Parse(*forward)
                if err != nil {
                        fmt.Printf("forward url.Parse failed: %v\n", err)
                        os.Exit(-1)
                }
                px, err := proxy.FromURL(url, proxy.Direct)
                tr := &http.Transport{
                        Dial:                  px.Dial,
                        TLSHandshakeTimeout:   10 * time.Second,
                        ExpectContinueTimeout: time.Second,
                        TLSClientConfig: &tls.Config{
                                InsecureSkipVerify: true,
                        },
                }
                p.SetRoundTripper(tr)
        } else {

                tr := &http.Transport{
                        Dial: (&net.Dialer{
                                Timeout:   30 * time.Second,
                                KeepAlive: 30 * time.Second,
                        }).Dial,
                        TLSHandshakeTimeout:   10 * time.Second,
                        ExpectContinueTimeout: time.Second,
                        TLSClientConfig: &tls.Config{
                                InsecureSkipVerify: true,
                        },
                }
                p.SetRoundTripper(tr)
        }

        l, err := net.Listen("tcp", *addr)
        if err != nil {
                log.Fatal(err)
        }

        log.Printf("Starting proxy on : %s", l.Addr().String())

        go p.Serve(l)

        sigc := make(chan os.Signal, 2)
        signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)

        <-sigc

        log.Println("martian: shutting down")
        os.Exit(0)
}

func init() {
        martian.Init()
}

from martian.

bramhaghosh avatar bramhaghosh commented on May 5, 2024

Fixed in #210

from martian.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.