Code Monkey home page Code Monkey logo

h2s's Introduction

h2s

GoDoc Release CircleCI Go Report Card License

h2s is a tiny CLI tool that wraps one or multiple HTTPS proxies into a SOCKS5 proxy. It does something like polipo and privoxy do, but in a reversed way.

There are already some SOCKS to HTTPS tools out there, but I can hardly find a reversed one (HTTPS to SOCKS), so I decided to make one on my own.

Install

You can view the release page for handy prebuilt binaries.

Build from Source

h2s relies on stadard libs only.

$ go get -u ekyu.moe/h2s/cmd/h2s

Configure

An example config file:

{
  // bind is the address h2s will listen to.
  // Note that since HTTPS proxy support only TCP, the h2s wrapped SOCKS5
  // proxy consequently support only TCP as well.
  "bind": "127.0.0.1:1080",

  // upstreams are HTTPS proxy upstreams.
  // h2s will do a simple round-robin load balance.
  "upstreams": [{
    // If no port is specified, 80 is assumed by default.
    "address": "proxy1.example.com",
  }, {
    "address": "proxy2.example.com:3128",

    // username and password are optional for proxy authentication.
    "username": "Alice",
    "password": "secret here"
  }, {
    // An HTTPS proxy over TLS upstream.
    // You have to specify port explicitly (usually 443), and set the tls field.
    "address": "secure.proxy.example.com:443",
    "username": "Secure",
    "password": "Yeah!",

    // h2s only provides some basic TLS settings. If you are an advanced user and
    // looking for other settings, you may use stunnel(1) to handle TLS instead,
    // and simply leave a naive TCP interface to h2s.
    "tlsConfig": {
      // If empty, serverName is set to the hostname from address.
      // Most users could just leave it empty.
      "serverName": "secure.proxy.example.com",

      // If you prefer to set a fingerprint instead of providing certs, you can
      // set this to true.
      // Do not set to true unless you know what you are doing.
      "insecureSkipVerify": false,
      // Server's SHA256 fingerprint, used to verify as an alternative to providing
      // the whole server certs, should be used with insecureSkipVerify to true.
      // If both rootCA and sha256Fingerprint are provided, they will both be
      // verified.
      //
      // An example to get fingerprint of a given cert:
      //     openssl x509 -fingerprint -sha256 -noout -in cert.cer | cut -f2 -d'=' | sed s/://g
      // or of a server with TLS enabled:
      //      openssl s_client -showcerts -connect example.com:443 < /dev/null | \
      //        openssl x509 -fingerprint -sha256 -noout | cut -f2 -d'=' | sed s/://g
      "sha256fingerprint": "22B975A1409850EF7F4522183E9C5A8955758FC899D70FE257112DA2FC430CCC",

      // rootCA is useful for self-signed certs. Be careful with it.
      // If the server has a trusted cert, you don't have to set it.
      "rootCA": "/path/to/the/ca/cert",

      // certFile and keyFile are advanced options for client authentication.
      // Most users could just leave it empty.
      "certFile": "/path/to/the/client/cert",
      "keyFile": "/path/to/the/client/key"
    }
  }],

  // accounts is an optional array of accounts for SOCKS5 authentication
  // with no accounts, authentication is disabled
  "accounts": [{
    "username": "test server",
    "password": "test"
  }],

  // timeout optionally sets timeout value when dialing to a upstream
  // default "20s"
  "timeout": "20s",
  // retries optionally specifies the max retries count of dialing to upstreams
  // default 3.
  "retries": 3
}

Usage

$ h2s [-config h2s.json]

License

MIT

h2s's People

Contributors

equim-chan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

h2s's Issues

非标准443端口WS+TLS 无法正常使用

V2Ray 配置如下
{
"inbound": {
"port": "1080",
"listen": "127.0.0.1",
"protocol": "socks",
"Settings": {
"auth": "noauth",
"udp": true
}
},
"outbound": {
"mux": {
"enabled": true
},
"protocol": "vmess",
"settings": {
"vnext": [{
"address": "127.0.0.1",
"port": 50001,
"users": [{
"id": "99b1b300-xxxxxxx48a0",
"alterId": 64
}]
}]
},
"streamSettings": {
"network": "ws",
"security": "tls",
"tlsSettings": {
"serverName": "xxx.com"
},
"wsSettings": {
"path": "/ray",
"headers": {
"Host": "xxx.com"
}
}
}
},
"inboundDetour": [{
"domainOverride": ["tls", "http"],
"listen": "127.0.0.1",
"port": 50001,
"protocol": "dokodemo-door",
"settings": {
"address": "xxx.com",
"port": 6443
},
"tag": "bridge"
}],
"outboundDetour": [{
"protocol": "socks",
"settings": {
"servers": [{
"address": "127.0.0.1",
"port": 1088
}]
},
"tag": "h2s"
}],
"routing": {
"settings": {
"rules": [{
"type": "field",
"inboundTag": ["bridge"],
"outboundTag": "h2s"
}],
"domainStrategy": "IPIfNonMatch"
},
"strategy": "rules"
}
}

H2S 配置如下

{
"bind": "127.0.0.1:1088",
"upstreams": [{
"address": "10.10.19.100:9092"
}],
"timeout": "20s",
"retries": 3
}

handshake: unexpected SOCKS version 0x43

公司提供http代理上网,微信PC客户端要设置socket代理才能用, 启动h2s, 微信代理到6668端口,报错了

{
  "bind": "127.0.0.1:6668",
  "upstreams": [{
    "address": "192.168.1.55:1047"
  }],
  "timeout": "20s",
  "retries": 3
}
PS D:\Program Files\h2s> .\h2s.exe -config h2s.json
2019/10/07 14:53:25.142806 Listening on 127.0.0.1:6668
2019/10/07 14:55:20.112300 h2s: serve: handshake: unexpected SOCKS version 0x43
2019/10/07 14:55:24.114171 h2s: serve: handshake: unexpected SOCKS version 0x43
2019/10/07 14:55:28.115574 h2s: serve: handshake: unexpected SOCKS version 0x43
2019/10/07 14:55:28.289528 h2s: serve: handshake: unexpected SOCKS version 0x47

麻烦作者看看可有解,thanks

与上游握手失败

您好,我想将学校提供的https代理转变为socks5代理,结果抛出了下面异常:
2019/03/08 10:28:25.489871 Listening on 127.0.0.1:2333
2019/03/08 10:28:34.606348 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:34.632784 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:34.635032 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:34.639377 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:34.641627 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:34.750934 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:34.759173 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:39.803129 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:39.810537 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:55.044235 h2s: serve: handshake upstream: unexpected EOF
2019/03/08 10:28:55.050157 h2s: serve: handshake upstream: unexpected EOF
配置文件为:
{
"bind": "127.0.0.1:2333",

"upstreams": [{
"address": "scholar-proxy.proxy.cn:3128",

"username": "username",                               
"password": "passwd"                               

}],

"timeout": "20s",
"retries": 3
}
我在wsl中使用H2S,然后在win下使用switchyomega 连接socks代理, switchyomega填入127.0.0.1:2333
请问该怎么解决呢?万分感谢!

tlsConfig 与示例json配置文件的字段名不同

代码中

h2s/h2s.go

Line 30 in 8b73394

TLSConfig *TLSConfig `json:"tlsConfig"`

与示例中

{
  "bind": "127.0.0.1:1080",
  "upstreams": [
    {
      "address": "proxy.example.com:443",
      "username": "user",
      "password": "pwd",
      "tls": {
         ......
       }
    }
  ],
 "accounts": [{
    "username": "test server",
    "password": "test"
  }],
  "timeout": "20s",
  "retries": 3
}

tls字段不同,测试必须写为tlsConfig才能识别

h2s: serve: handshake upstream: 403 Access violation

配置如下:

{
  "log": {
    "loglevel": "debug"
  },
  "inbounds": [{
    "port": 1090,
    "listen": "127.0.0.1",
    "tag": "socks-inbound",
    "protocol": "socks",
    "settings": {
      "auth": "noauth",
      "udp": false,
      "ip": "127.0.0.1"
    }
  }],
  "outbound": 
  {
    "protocol": "vmess",
    "settings": {
      "vnext": [
        {
          "address": "x.x.x.x", 
          "port": 8888,
          "users": [
            {
              "id": "c5f1ecdb-07d5-4834-ac30-83433804c8af",  
              "alterId": 64 
            }
          ]
        }
      ]
    },
    "proxySettings": {
      "tag": "h2s"  
    },
    "tag":"proxy"
  },

  "outboundDetour": [
    {
      "protocol": "socks",
      "settings":{
        "servers":[{
          "address":"127.0.0.1",
          "port":3080
        }]
      },
      "tag": "h2s"
    }
  ]
}

v2ray提示:

v2ray.com/core/app/proxyman/inbound: connection ends 
> v2ray.com/core/proxy/socks: connection ends
 > v2ray.com/core/proxy/socks: failed to transport all TCP response 
> io: read/write on closed pipe

h2s提示:

h2s: serve: handshake upstream: 403 Access violation

是哪里配置不对么?

配置文件域验证问题

{
"bind": "127.0.0.1:8088",
"upstreams": [{
"address": "10.14.1.10:8080",
"username": "domain### m",
"password": "password"
}],
"timeout": "20s",
"retries": 3
}

使用这个配置文件会报一下错误

2018/07/25 16:20:54.767080 parse config: invalid character 'm' in string escape code

因为用户名第一位是m开头

Please support ipv6

we were using ipv6 , but we have problem with h2s

2019/03/04 16:18:12.772507 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:13.863631 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:14.445864 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:14.560087 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:14.791805 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:16.867652 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:17.780151 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:18.875932 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:18.875946 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:19.782748 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:19.782799 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:20.874084 h2s: serve: read request: unexpected command 0x03
2019/03/04 16:18:21.782399 h2s: serve: read request: unexpected command 0x03

关于 h2s 与 v2ray 进行代理链拼接的说明与讨论

如果你想让 h2s 与 v2ray 以代理链(传出代理)的方式配合使用的话,那么就需要注意一些地方。

首先必须要明确的一点是,无论如何 h2s 都只接受 TCP 的输入,因为 HTTP 代理只能处理 TCP。

假如你原先的 v2ray 配置没有使用自定义的传输,即 streamSettings 为默认值,也就是朴素的 TCP (朴素 VMess)的话,那么直接将 h2s 的监听地址设为一个 outboundDetour 再设置传出代理 proxySettings 桥接过去即可,下文就可以不用看了。而如果你设置了 streamSettings,比如配置了 TLS 或/与 WS 的话,就必须要进行额外的配置才可正常使用,也就是下文要讲的内容。

原理上说,之所以在这种情况下不能直接设置 proxySettings,是因为 v2ray 目前(v3.15)不能同时应用 streamSettingsproxySettings,即,这两个设置是互斥的,只有一个能生效。这是在 v2ray 的文档上就有写的:

proxySettings: 传出代理配置。当传出代理生效时,此传出协议的 streamSettings 将不起作用。

这大概是因为 v2ray 的设计者没有考虑到将传输层也可以作为代理链数据的一部分发给下游,或者是出于代理模型本身的考虑(比如 h2s 无法处理 UDP,那么就不能将 mKCP 的 stream 传给下游,这是在设计上就不允许的)。总之,我们现在需要解决两个设置冲突的问题。否则 streamSettings 无法生效的话,TLS/WS 也就无法起作用了。从表面上看,结果会表现为 v2ray 报协议头错误(如果是 VMess + TLS + WS 的话就是 VMess 头错误)。

这里我假定你的代理模式是 VMess + TLS + WS,并且已完成了符合“传出将直连至服务端”这一场景的配置,现在要让这个协议栈与 h2s 合为代理链。即

v2ray 客户端 ---h2s 的 SOCKS5---> 内网 HTTP 代理服务器 ---VMess + TLS + WS (或类似配置)---> v2ray 服务端

那么就要在原有配置文件的基础上作出一些修改。

以下为上述场景中 v2ray 的参考配置。省去了一些冗余的部分,只提重点。

{
  "inbound": {
    // 你原先的 inbound,无需改动
    // ...
  },

  "outbound": {
    "protocol": "vmess",
    "settings": {
      "vnext": [{
        "address": "127.0.0.1", // 注意这里
        "port": 50001,          // 注意这里。端口可自定义,与下方任意门的相同即可
        "users": [{
          // 此处与原先的配置相同
          // ...
        }]
      }]
    },
    "streamSettings": {
      "network": "ws",
      "security": "tls",
      "wsSettings": {
        // path 等设置与原先的相同
        // ...
        "headers": {
          "Host": "你的主机名(一般是域名)" // 请务必正确地配置这段,否则 WS 握手会失败
        }
      },
      "tlsSettings": {
        // ...
        "serverName": "你的主机名(一般是域名)", // 请务必正确地配置这段,否则 TLS 握手会失败。一般的,它与上述的 Host 相同
      }
    }
  },

  // 新加入的任意门 inbound,用于内部桥接
  "inboundDetour": [{
    "listen": "127.0.0.1",
    "port": 50001, // 与上面的 VMess 的 port 相同即可
    "protocol": "dokodemo-door",
    "settings": {
      "network": "tcp", // h2s 只接受 TCP
      "address": "在此填上原本应该填在 VMess 的 address 里的内容", // 注意这里,一般来说就是你的 v2ray 服务端地址
      "port": 443 // 在此填上原本应该填在 VMess 的 port 里的内容。同上
    },
    "tag": "内部桥接点" // tag 是必须要有的,否则无法进行路由
  }],

  // 此 outbound 连接至 h2s
  "outboundDetour": [{
    "protocol": "socks",
    "settings": {
      "servers": [{
        // 此处参照 h2s 的配置进行配置即可
        // ...
      }]
    },
    "tag": "h2s" // tag 是必须要有的,否则无法进行路由
  }],

  "routing": {
    "strategy": "rules",
    "settings": {
      // 添加下面这条规则
      "rules": [{
        "type": "field",
        "inboundTag": ["内部桥接点"],
        "outboundTag": "h2s"
      }],
      // 其余部分不变
      // ...
    }
  }
}

如果在 h2s 与 v2ray 的桥接问题上有疑问,请在本 issue 中提出。

V2ray+WS+TLS配置文件排错

我这个配置文件昨天还可以用,今天就不能正常使用了,非常奇怪。

Vray

{
"inbound": {
"port": 1080,
"listen": "0.0.0.0",
"protocol": "socks",
"settings": {
"auth": "noauth",
"udp": true
}
},

"outbound": {
	"mux": {
		"enabled": true
	},
	"protocol": "vmess",
	"settings": {
		"vnext": [{
			"address": "127.0.0.1",
			"port": 50001,
			"users": [{
				"id": "UUID",
				"alterId": 64
			}]
		}]
	},
	"streamSettings": {
		"network": "ws",
		"security": "tls",
		"tlsSettings": {
			"serverName": "xxx.com"
		},
		"wsSettings": {
			"path": "/path",
			"headers": {
				"Host": "xxx.com"
			}
		}
	}
},

"inboundDetour": [{
	"domainOverride": ["tls", "http"],
	"listen": "127.0.0.1",
	"port": 50001,
	"protocol": "dokodemo-door",
	"settings": {
		"address": "xxx.com",
		"port": 443
	},
	"tag": "内部桥接点"
}],

"outboundDetour": [{
		"protocol": "socks",
		"settings": {
			"servers": [{
				"address": "127.0.0.1",
				"port": 1088
			}]
		},
		"tag": "h2s"
	}
],
"routing": {
	"settings": {
		"rules": [{
				"type": "field",
				"inboundTag": ["内部桥接点"],
				"outboundTag": "h2s"
			}
		],
		"domainStrategy": "IPIfNonMatch"
	},
	"strategy": "rules"
}

}

H2S

{
"bind": "127.0.0.1:1088",
"upstreams": [{
"address": "192.168.99.100:9092"
}],
"timeout": "20s",
"retries": 3
}

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.