commit
22843aa300
@ -0,0 +1,2 @@ |
||||
export GOPATH=`pwd` |
||||
go build -o bin/gomitmproxy src/main/*.go |
@ -1,71 +0,0 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"bufio" |
||||
"bytes" |
||||
"compress/flate" |
||||
"compress/gzip" |
||||
"fmt" |
||||
"io/ioutil" |
||||
"math" |
||||
"net/http" |
||||
"strconv" |
||||
) |
||||
|
||||
func httpDump(req *http.Request, resp *http.Response) { |
||||
defer resp.Body.Close() |
||||
var respStatusStr string |
||||
respStatus := resp.StatusCode |
||||
respStatusHeader := int(math.Floor(float64(respStatus / 100))) |
||||
switch respStatusHeader { |
||||
case 2: |
||||
respStatusStr = Green("<--" + strconv.Itoa(respStatus)) |
||||
case 3: |
||||
respStatusStr = Yellow("<--" + strconv.Itoa(respStatus)) |
||||
case 4: |
||||
respStatusStr = Magenta("<--" + strconv.Itoa(respStatus)) |
||||
case 5: |
||||
respStatusStr = Red("<--" + strconv.Itoa(respStatus)) |
||||
} |
||||
fmt.Println(Green("Request:")) |
||||
fmt.Printf("%s %s %s\n", Blue(req.Method), req.RequestURI, respStatusStr) |
||||
for headerName, headerContext := range req.Header { |
||||
fmt.Printf("%s: %s\n", Blue(headerName), headerContext) |
||||
} |
||||
fmt.Println(Green("Response:")) |
||||
for headerName, headerContext := range resp.Header { |
||||
fmt.Printf("%s: %s\n", Blue(headerName), headerContext) |
||||
} |
||||
|
||||
respBody, err := ioutil.ReadAll(resp.Body) |
||||
if err != nil { |
||||
logger.Println("func httpDump read resp body err:", err) |
||||
} else { |
||||
acceptEncode := resp.Header["Content-Encoding"] |
||||
var respBodyBin bytes.Buffer |
||||
w := bufio.NewWriter(&respBodyBin) |
||||
w.Write(respBody) |
||||
w.Flush() |
||||
for _, compress := range acceptEncode { |
||||
switch compress { |
||||
case "gzip": |
||||
r, err := gzip.NewReader(&respBodyBin) |
||||
if err != nil { |
||||
logger.Println("gzip reader err:", err) |
||||
} else { |
||||
defer r.Close() |
||||
respBody, _ = ioutil.ReadAll(r) |
||||
} |
||||
break |
||||
case "deflate": |
||||
r := flate.NewReader(&respBodyBin) |
||||
defer r.Close() |
||||
respBody, _ = ioutil.ReadAll(r) |
||||
break |
||||
} |
||||
} |
||||
fmt.Printf("%s\n", string(respBody)) |
||||
} |
||||
|
||||
fmt.Printf("%s%s%s\n", Black("####################"), Cyan("END"), Black("####################")) |
||||
} |
@ -1,84 +0,0 @@ |
||||
// This example shows a proxy server that uses go-mitm to man-in-the-middle
|
||||
// HTTPS connections opened with CONNECT requests
|
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"flag" |
||||
"log" |
||||
"net/http" |
||||
"os" |
||||
"sync" |
||||
"time" |
||||
) |
||||
|
||||
const ( |
||||
Version = "1.1" |
||||
) |
||||
|
||||
var ( |
||||
wg sync.WaitGroup |
||||
) |
||||
|
||||
var logFile *os.File |
||||
var logger *log.Logger |
||||
|
||||
func main() { |
||||
var conf Cfg |
||||
|
||||
conf.Port = flag.String("port", "8080", "Listen port") |
||||
conf.Raddr = flag.String("raddr", "", "Remote addr") |
||||
conf.Log = flag.String("log", "./error.log", "log file path") |
||||
conf.Monitor = flag.Bool("m", false, "monitor mode") |
||||
conf.Tls = flag.Bool("tls", false, "tls connect") |
||||
flag.Parse() |
||||
|
||||
var err error |
||||
logFile, err = os.Create(*conf.Log) |
||||
if err != nil { |
||||
log.Fatalln("fail to create log file!") |
||||
} |
||||
|
||||
logger = log.New(logFile, "[gomitmproxy]", log.LstdFlags|log.Llongfile) |
||||
|
||||
wg.Add(1) |
||||
gomitmproxy(&conf) |
||||
wg.Wait() |
||||
} |
||||
|
||||
func gomitmproxy(conf *Cfg) { |
||||
tlsConfig := NewTlsConfig("gomitmproxy-ca-pk.pem", "gomitmproxy-ca-cert.pem", "", "") |
||||
|
||||
handler, err := InitConfig(conf, tlsConfig) |
||||
if err != nil { |
||||
logger.Fatalf("InitConfig error: %s", err) |
||||
} |
||||
|
||||
server := &http.Server{ |
||||
Addr: ":" + *conf.Port, |
||||
Handler: handler, |
||||
ReadTimeout: 1 * time.Hour, |
||||
WriteTimeout: 1 * time.Hour, |
||||
} |
||||
|
||||
go func() { |
||||
log.Printf("proxy listening port:%s", *conf.Port) |
||||
|
||||
if *conf.Tls { |
||||
log.Println("ListenAndServeTLS") |
||||
err = server.ListenAndServeTLS("gomitmproxy-ca-cert.pem", "gomitmproxy-ca-pk.pem") |
||||
} else { |
||||
log.Println("ListenAndServe") |
||||
err = server.ListenAndServe() |
||||
} |
||||
if err != nil { |
||||
logger.Fatalf("Unable to start HTTP proxy: %s", err) |
||||
} |
||||
|
||||
wg.Done() |
||||
|
||||
log.Printf("gomitmproxy stop!!!!") |
||||
}() |
||||
|
||||
return |
||||
} |
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
@ -0,0 +1,41 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"config" |
||||
"flag" |
||||
"io" |
||||
"mitm" |
||||
"mylog" |
||||
"os" |
||||
"sync" |
||||
) |
||||
|
||||
func main() { |
||||
var log io.WriteCloser |
||||
var err error |
||||
// cofig
|
||||
conf := new(config.Cfg) |
||||
conf.Port = flag.String("port", "8080", "Listen port") |
||||
conf.Raddr = flag.String("raddr", "", "Remote addr") |
||||
conf.Log = flag.String("logFile", "", "log file path") |
||||
conf.Monitor = flag.Bool("m", false, "monitor mode") |
||||
conf.Tls = flag.Bool("tls", false, "tls connect") |
||||
flag.Parse() |
||||
|
||||
// init log
|
||||
if *conf.Log != "" { |
||||
log, err = os.Create(*conf.Log) |
||||
if err != nil { |
||||
mylog.Fatalln("fail to create log file!") |
||||
} |
||||
} else { |
||||
log = os.Stderr |
||||
} |
||||
mylog.SetLog(log) |
||||
|
||||
// start mitm proxy
|
||||
wg := new(sync.WaitGroup) |
||||
wg.Add(1) |
||||
mitm.Gomitmproxy(conf, wg) |
||||
wg.Wait() |
||||
} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
package color |
||||
|
||||
import ( |
||||
"fmt" |
@ -1,16 +1,6 @@ |
||||
package main |
||||
package config |
||||
|
||||
import ( |
||||
"crypto/tls" |
||||
"time" |
||||
) |
||||
|
||||
const ( |
||||
ONE_DAY = 24 * time.Hour |
||||
TWO_WEEKS = ONE_DAY * 14 |
||||
ONE_MONTH = 1 |
||||
ONE_YEAR = 1 |
||||
) |
||||
import "crypto/tls" |
||||
|
||||
type Cfg struct { |
||||
Port *string |
@ -1,6 +1,6 @@ |
||||
// package cache implements a really primitive cache that associates expiring
|
||||
// values with string keys. This cache never clears itself out.
|
||||
package main |
||||
package mitm |
||||
|
||||
import ( |
||||
"sync" |
@ -0,0 +1,100 @@ |
||||
package mitm |
||||
|
||||
import ( |
||||
"bufio" |
||||
"bytes" |
||||
"color" |
||||
"compress/flate" |
||||
"compress/gzip" |
||||
"fmt" |
||||
"io" |
||||
"io/ioutil" |
||||
"math" |
||||
"mylog" |
||||
"net/http" |
||||
"strconv" |
||||
) |
||||
|
||||
func httpDump(reqDump []byte, resp *http.Response) { |
||||
defer resp.Body.Close() |
||||
var respStatusStr string |
||||
respStatus := resp.StatusCode |
||||
respStatusHeader := int(math.Floor(float64(respStatus / 100))) |
||||
switch respStatusHeader { |
||||
case 2: |
||||
respStatusStr = color.Green("<--" + strconv.Itoa(respStatus)) |
||||
case 3: |
||||
respStatusStr = color.Yellow("<--" + strconv.Itoa(respStatus)) |
||||
case 4: |
||||
respStatusStr = color.Magenta("<--" + strconv.Itoa(respStatus)) |
||||
case 5: |
||||
respStatusStr = color.Red("<--" + strconv.Itoa(respStatus)) |
||||
} |
||||
|
||||
fmt.Println(color.Green("Request:"), respStatusStr) |
||||
req, _ := ParseReq(reqDump) |
||||
fmt.Printf("%s %s %s\n", color.Blue(req.Method), req.Host+req.RequestURI, respStatusStr) |
||||
fmt.Printf("%s %s\n", color.Blue("RemoteAddr:"), req.RemoteAddr) |
||||
for headerName, headerContext := range req.Header { |
||||
fmt.Printf("%s: %s\n", color.Blue(headerName), headerContext) |
||||
} |
||||
|
||||
if req.Method == "POST" { |
||||
fmt.Println(color.Green("POST Param:")) |
||||
err := req.ParseForm() |
||||
if err != nil { |
||||
mylog.Println("parseForm error:", err) |
||||
} else { |
||||
for k, v := range req.Form { |
||||
fmt.Printf("\t%s: %s\n", color.Blue(k), v) |
||||
} |
||||
} |
||||
} |
||||
fmt.Println(color.Green("Response:")) |
||||
for headerName, headerContext := range resp.Header { |
||||
fmt.Printf("%s: %s\n", color.Blue(headerName), headerContext) |
||||
} |
||||
|
||||
respBody, err := ioutil.ReadAll(resp.Body) |
||||
if err != nil { |
||||
mylog.Println("func httpDump read resp body err:", err) |
||||
} else { |
||||
acceptEncode := resp.Header["Content-Encoding"] |
||||
var respBodyBin bytes.Buffer |
||||
w := bufio.NewWriter(&respBodyBin) |
||||
w.Write(respBody) |
||||
w.Flush() |
||||
for _, compress := range acceptEncode { |
||||
switch compress { |
||||
case "gzip": |
||||
r, err := gzip.NewReader(&respBodyBin) |
||||
if err != nil { |
||||
mylog.Println("gzip reader err:", err) |
||||
} else { |
||||
defer r.Close() |
||||
respBody, _ = ioutil.ReadAll(r) |
||||
} |
||||
break |
||||
case "deflate": |
||||
r := flate.NewReader(&respBodyBin) |
||||
defer r.Close() |
||||
respBody, _ = ioutil.ReadAll(r) |
||||
break |
||||
} |
||||
} |
||||
fmt.Printf("%s\n", string(respBody)) |
||||
} |
||||
|
||||
fmt.Printf("%s%s%s\n", color.Black("####################"), color.Cyan("END"), color.Black("####################")) |
||||
} |
||||
|
||||
func ParseReq(b []byte) (*http.Request, error) { |
||||
// func ReadRequest(b *bufio.Reader) (req *Request, err error) { return readRequest(b, deleteHostHeader) }
|
||||
fmt.Println(string(b)) |
||||
fmt.Println("-----------------------") |
||||
var buf io.ReadWriter |
||||
buf = new(bytes.Buffer) |
||||
buf.Write(b) |
||||
bufr := bufio.NewReader(buf) |
||||
return http.ReadRequest(bufr) |
||||
} |
@ -0,0 +1,48 @@ |
||||
// This example shows a proxy server that uses go-mitm to man-in-the-middle
|
||||
// HTTPS connections opened with CONNECT requests
|
||||
|
||||
package mitm |
||||
|
||||
import ( |
||||
"config" |
||||
"mylog" |
||||
"net/http" |
||||
"sync" |
||||
"time" |
||||
) |
||||
|
||||
func Gomitmproxy(conf *config.Cfg, wg *sync.WaitGroup) { |
||||
tlsConfig := config.NewTlsConfig("gomitmproxy-ca-pk.pem", "gomitmproxy-ca-cert.pem", "", "") |
||||
|
||||
handler, err := InitConfig(conf, tlsConfig) |
||||
if err != nil { |
||||
mylog.Fatalf("InitConfig error: %s", err) |
||||
} |
||||
|
||||
server := &http.Server{ |
||||
Addr: ":" + *conf.Port, |
||||
Handler: handler, |
||||
ReadTimeout: 1 * time.Hour, |
||||
WriteTimeout: 1 * time.Hour, |
||||
} |
||||
|
||||
go func() { |
||||
mylog.Printf("Gomitmproxy Listening On: %s", *conf.Port) |
||||
if *conf.Tls { |
||||
mylog.Println("Listen And Serve HTTP TLS") |
||||
err = server.ListenAndServeTLS("gomitmproxy-ca-cert.pem", "gomitmproxy-ca-pk.pem") |
||||
} else { |
||||
mylog.Println("Listen And Serve HTTP") |
||||
err = server.ListenAndServe() |
||||
} |
||||
if err != nil { |
||||
mylog.Fatalf("Unable To Start HTTP proxy: %s", err) |
||||
} |
||||
|
||||
wg.Done() |
||||
|
||||
mylog.Printf("Gomitmproxy Stop!!!!") |
||||
}() |
||||
|
||||
return |
||||
} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
package mitm |
||||
|
||||
import ( |
||||
"io" |
@ -0,0 +1,30 @@ |
||||
package mylog |
||||
|
||||
import "log" |
||||
import "io" |
||||
|
||||
var logger *log.Logger |
||||
|
||||
func SetLog(l io.WriteCloser) { |
||||
logger = log.New(l, "[gomitmproxy]", log.LstdFlags) |
||||
} |
||||
|
||||
func Fatalf(format string, v ...interface{}) { |
||||
logger.Fatalf(format, v) |
||||
} |
||||
|
||||
func Fatalln(v ...interface{}) { |
||||
logger.Fatalln(v) |
||||
} |
||||
|
||||
func Printf(format string, v ...interface{}) { |
||||
logger.Printf(format, v) |
||||
} |
||||
|
||||
func Println(v ...interface{}) { |
||||
logger.Println(v) |
||||
} |
||||
|
||||
func Panicln(v ...interface{}) { |
||||
logger.Panicln(v) |
||||
} |
@ -0,0 +1,17 @@ |
||||
package mylog |
||||
|
||||
import ( |
||||
"log" |
||||
"os" |
||||
"testing" |
||||
) |
||||
|
||||
func TestMyLog(t *testing.T) { |
||||
logFile, err := os.Create("test.log") |
||||
if err != nil { |
||||
log.Fatalln("fail to create log file!") |
||||
} |
||||
logger := log.New(logFile, "[gomitmproxy]", log.LstdFlags|log.Llongfile) |
||||
SetLog(logger) |
||||
Println("log test") |
||||
} |
@ -0,0 +1 @@ |
||||
[gomitmproxy]2017/04/03 00:00:25 /Users/bao/program/go/gowork/gomitmproxy/src/vendor/mylog/my_log.go:20: [log test] |
Loading…
Reference in new issue