Commit 4a78f9ab authored by wang's avatar wang

cronet

parent 449ed648
package database
import (
"encoding/json"
"github.com/sirupsen/logrus"
"tls-forward/model"
)
func NewRequestLog(item model.ForwardItem) uint {
marshal, err := json.Marshal(item.Headers)
if err != nil {
logrus.Errorf("序列化header 失败 %s", err.Error())
}
logItem := RequestLog{
Proxy: item.Proxy,
Method: item.Method,
Headers: string(marshal),
Url: item.Url,
Data: item.Data,
Timeout: item.Timeout,
AllowRedirects: item.AllowRedirects,
Verify: item.Verify,
Debug: item.Debug,
RandomTls: item.RandomTls,
StatusCode: 0,
Text: "",
}
result := MysqlDb.Create(&logItem) // pass pointer of data to Create
if result.Error != nil {
logrus.Errorf("log插入失败 %s", result.Error.Error())
return 0
}
return logItem.ID
}
func UpdateLogInfo(id uint, statusCode int, text string) {
tx := MysqlDb.Table("request_logs").Where("id = ?", id).Update("status_code", statusCode).Update("text", text)
if tx.Error != nil {
logrus.Errorf("更新记录失败 %s", tx.Error.Error())
}
}
package database
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type RequestLog struct {
gorm.Model
Proxy string `json:"proxy,omitempty"`
Method string `json:"method,omitempty"`
Headers string `json:"headers,omitempty"`
Url string `json:"url,omitempty"`
Data string `json:"data,omitempty"`
Timeout int `json:"timeout,omitempty"`
AllowRedirects bool `json:"allow_redirects,omitempty"`
Verify bool `json:"verify,omitempty"`
Debug bool `json:"debug"`
RandomTls bool `json:"random_tls"`
StatusCode int
Text string
}
func (r RequestLog) Schema() string {
//TODO implement me
return "request_log"
}
func (r RequestLog) Name() string {
//TODO implement me
return "request_log"
}
var MysqlDb *gorm.DB
func init() {
dsn := "root:123456@tcp(192.168.50.110:3306)/tls?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
db.AutoMigrate(&RequestLog{})
if err != nil {
panic(err)
}
MysqlDb = db
}
......@@ -11,6 +11,7 @@ require (
)
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/bogdanfinn/fhttp v0.5.27 // indirect
......@@ -22,7 +23,10 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
......@@ -39,9 +43,11 @@ require (
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/gorm v1.25.11 // indirect
)
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
......@@ -48,12 +50,19 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM=
......@@ -139,6 +148,8 @@ golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
......@@ -153,4 +164,9 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
......@@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"time"
"tls-forward/tls"
)
......@@ -44,9 +45,10 @@ func main() {
// 是否需要压缩滚动日志, 使用的 gzip 压缩
Compress: true, // disabled by default
}
logrus.SetOutput(logWriter)
fmt.Println(logWriter)
//logrus.SetOutput(logWriter)
//logrus.SetOutput(io.MultiWriter(logWriter, os.Stdout))
//logrus.SetOutput(os.Stdout)
logrus.SetOutput(os.Stdout)
fmt.Println("VERSION 1.0.1")
r := gin.New() //创建一个默认的路由引擎
r.LoadHTMLFiles("tls-froward/dist/index.html")
......
package model
type ForwardItem struct {
Proxy string `json:"proxy,omitempty"`
Method string `json:"method,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Url string `json:"url,omitempty"`
Data string `json:"data,omitempty"`
Timeout int `json:"timeout,omitempty"`
AllowRedirects bool `json:"allow_redirects,omitempty"`
Verify bool `json:"verify,omitempty"`
Debug bool `json:"debug"`
RandomTls bool `json:"random_tls"`
}
......@@ -16,18 +16,21 @@ import (
"math/big"
"net/url"
"strings"
"tls-forward/model"
)
type Client struct {
Client tls_client.HttpClient
Jar tls_client.CookieJar
Item ForwardItem
Item model.ForwardItem
Response *http.Response
uri *url.URL
TlsVersion string
LogId uint
}
func (c *Client) Start() error {
var req *http.Request
var err error
uri, err := url.Parse(c.Item.Url)
......@@ -75,12 +78,14 @@ func (c *Client) Start() error {
for k, v := range c.Item.Headers {
req.Header.Set(k, v)
}
//c.Client.SetProxy("http://user-unifflcc-region-us:q39CEBTs5A5YQXor@pr.roxlabs.cn:4600")
resp, err := c.Client.Do(req)
if err != nil {
logrus.Error("请求失败 ", err.Error())
return err
}
c.Response = resp
return nil
}
......@@ -141,7 +146,7 @@ func RandRandom(a int64) int64 {
res, _ := rand.Int(rand.Reader, big.NewInt(a))
return res.Int64()
}
func NewClient(forWard ForwardItem, ios bool) (Client, error) {
func NewClient(forWard model.ForwardItem, ios bool) (Client, error) {
jar := tls_client.NewCookieJar()
tlsVersion := chromes[RandRandom(int64(len(chromes)))]
if ios {
......@@ -170,7 +175,6 @@ func NewClient(forWard ForwardItem, ios bool) (Client, error) {
if err != nil {
return Client{}, err
}
return Client{Client: client, Item: forWard, Jar: jar, TlsVersion: tlsVersion.GetClientHelloStr()}, nil
}
......
......@@ -5,23 +5,12 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"time"
"tls-forward/database"
"tls-forward/model"
)
type ForwardItem struct {
Proxy string `json:"proxy,omitempty"`
Method string `json:"method,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Url string `json:"url,omitempty"`
Data string `json:"data,omitempty"`
Timeout int `json:"timeout,omitempty"`
AllowRedirects bool `json:"allow_redirects,omitempty"`
Verify bool `json:"verify,omitempty"`
Debug bool `json:"debug"`
RandomTls bool `json:"random_tls"`
}
func ForwardIOS(c *gin.Context) {
var forWard ForwardItem
var forWard model.ForwardItem
if err := c.ShouldBindJSON(&forWard); err != nil {
logrus.Error(err.Error())
c.JSON(400, gin.H{
......@@ -33,7 +22,11 @@ func ForwardIOS(c *gin.Context) {
logrus.Infof("开始转发 url: %s proxy: %s %v", forWard.Url, forWard.Proxy, forWard.AllowRedirects)
t1 := time.Now()
// 开始转发
client, err := NewClient(forWard, true)
defer func() {
client.LogId = database.NewRequestLog(forWard)
}()
if err != nil {
logrus.Error("client初始化失败 ", err.Error())
c.JSON(400, gin.H{
......@@ -43,6 +36,7 @@ func ForwardIOS(c *gin.Context) {
}
if err := client.Start(); err != nil {
logrus.Error("请求失败 ", err.Error())
database.UpdateLogInfo(client.LogId, -1, "请求失败 "+err.Error())
c.JSON(400, gin.H{
"msg": "请求失败 " + err.Error(),
})
......@@ -65,12 +59,13 @@ func ForwardIOS(c *gin.Context) {
return
}
}()
client.Client.CloseIdleConnections()
logrus.Infof("%s 转发 %s %s %d 耗时 %s", client.TlsVersion, forWard.Url, forWard.Proxy, client.GetStatusCode(), time.Now().Sub(t1).String())
c.JSON(200, res)
}
func Forward(c *gin.Context) {
var forWard ForwardItem
var forWard model.ForwardItem
if err := c.ShouldBindJSON(&forWard); err != nil {
logrus.Error(err.Error())
c.JSON(400, gin.H{
......@@ -83,6 +78,11 @@ func Forward(c *gin.Context) {
t1 := time.Now()
// 开始转发
client, err := NewClient(forWard, false)
client.LogId = database.NewRequestLog(forWard)
//defer func() {
//}()
//
if err != nil {
logrus.Error("client初始化失败 ", err.Error())
c.JSON(400, gin.H{
......@@ -91,9 +91,12 @@ func Forward(c *gin.Context) {
return
}
if err := client.Start(); err != nil {
logrus.Error("请求失败 ", err.Error())
v := fmt.Sprintf("请求失败 %s", err.Error())
logrus.Error(v)
database.UpdateLogInfo(client.LogId, -1, v)
c.JSON(400, gin.H{
"msg": "请求失败 " + err.Error(),
"msg": v,
})
return
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment