chisel README

The project's README from github.com/jpillora/chisel. As one markdown file: README.md.

Chisel

GoDoc CI

Chisel is a fast TCP/UDP tunnel, transported over HTTP, secured via SSH. Single executable including both client and server. Written in Go (golang). Chisel is mainly useful for passing through firewalls, though it can also be used to provide a secure endpoint into your network.

overview

Table of Contents

Features

Install

Binaries

Releases Releases

See the latest release or download and install it now with curl https://i.jpillora.com/chisel! | bash

Binaries are built with the latest Go release, which sets the minimum OS versions: Windows 10 / Server 2016, macOS 12, Linux kernel 3.2, FreeBSD 12.2. For older systems (e.g. Windows 7), use release v1.8.1 or earlier.

Docker

Docker Pulls Image Size

docker run --rm -it jpillora/chisel --help

Images are multi-arch and published to both Docker Hub (jpillora/chisel) and GitHub Container Registry (ghcr.io/jpillora/chisel).

Fedora

The package is maintained by the Fedora community. If you encounter issues related to the usage of the RPM, please use this issue tracker.

sudo dnf -y install chisel

Source

$ go install github.com/jpillora/chisel@latest

Demo

You can run your own demo server in minutes (the old Heroku demo went away with Heroku's free tier). example/fly.toml deploys this chisel server to fly.io's free allowance:

$ chisel server --port $PORT --backend http://example.com
# listens on $PORT, proxies normal web requests to http://example.com

Deploy it with fly launch --copy-config from the example/ directory, then tunnel to any service running beside the server, e.g.:

$ chisel client https://<your-app>.fly.dev 3000
# connects to your chisel server,
# tunnels your localhost:3000 to the server's localhost:3000

Visiting your app's URL in a browser hits the server's default backend proxy and shows a copy of example.com.

Usage

$ chisel --help

  Usage: chisel [command] [--help]

  Version: X.Y.Z

  Commands:
    server - runs chisel in server mode
    client - runs chisel in client mode

  Read more:
    https://github.com/jpillora/chisel
$ chisel server --help

  Usage: chisel server [options]

  Options:

    --host, Defines the HTTP listening host – the network interface
    (defaults the environment variable HOST and falls back to 0.0.0.0).

    --port, -p, Defines the HTTP listening port (defaults to the environment
    variable PORT and falls back to port 8080).

    --key, (deprecated use --keygen and --keyfile instead)
    An optional string to seed the generation of a ECDSA public
    and private key pair. All communications will be secured using this
    key pair. Share the subsequent fingerprint with clients to enable detection
    of man-in-the-middle attacks (defaults to the CHISEL_KEY environment
    variable, otherwise a new key is generate each run).

    --keygen, A path to write a newly generated PEM-encoded SSH private key file.
    If users depend on your --key fingerprint, you may also include your --key to
    output your existing key. Use - (dash) to output the generated key to stdout.

    --keyfile, An optional path to a PEM-encoded SSH private key. When
    this flag is set, the --key option is ignored, and the provided private key
    is used to secure all communications. (defaults to the CHISEL_KEY_FILE
    environment variable). Since ECDSA keys are short, you may also set keyfile
    to the inline key string itself, exactly as printed by --keygen (a base64
    string with a "ck-" prefix); no extra base64 encoding is needed.

    --authfile, An optional path to a users.json file. This file should
    be an object with users defined like:
      {
        "<user:pass>": ["<addr-regex>","<addr-regex>"]
      }
    when <user> connects, their <pass> will be verified and then
    each of the remote addresses will be compared against the list
    of address regular expressions for a match. Patterns are NOT
    anchored by default: "10.0.0.1:80" also matches
    "210.0.0.1:8080", and "." matches any character. Anchor your
    patterns, e.g. "^10\.0\.0\.1:80$". The empty string ""
    matches every address. Addresses will
    always come in the form "<remote-host>:<remote-port>" for normal remotes,
    "R:<local-interface>:<local-port>" for reverse port forwarding
    remotes, and "socks" for SOCKS5 proxy access. Note that SOCKS5
    access previously bypassed this list; existing authfiles which
    should allow SOCKS5 must add an entry matching "socks" (the
    empty wildcard "" matches everything, including "socks"). This
    file will be automatically reloaded on change. Reloads apply
    to new connections and to new tunnels of connected clients;
    established tunnels are not interrupted.

    --auth, An optional string representing a single user with full
    access, in the form of <user:pass>. It is equivalent to creating an
    authfile with {"<user:pass>": [""]}. If unset, it will use the
    environment variable AUTH.

    --keepalive, An optional keepalive interval. Since the underlying
    transport is HTTP, in many instances we'll be traversing through
    proxies, often these proxies will close idle connections. You must
    specify a time with a unit, for example '5s' or '2m'. Defaults
    to '25s' (set to 0s to disable).

    --backend, Specifies another HTTP server to proxy requests to when
    chisel receives a normal HTTP request. Useful for hiding chisel in
    plain sight. --proxy is accepted as an alias for this flag.

    --socks5, Allow clients to access the internal SOCKS5 proxy. See
    chisel client --help for more information.

    --reverse, Allow clients to specify reverse port forwarding remotes
    in addition to normal remotes.

    --tls-key, Enables TLS and provides optional path to a PEM-encoded
    TLS private key. When this flag is set, you must also set --tls-cert,
    and you cannot set --tls-domain.

    --tls-cert, Enables TLS and provides optional path to a PEM-encoded
    TLS certificate. When this flag is set, you must also set --tls-key,
    and you cannot set --tls-domain.

    --tls-domain, Enables TLS and automatically acquires a TLS key and
    certificate using LetsEncrypt. Setting --tls-domain requires port 443.
    You may specify multiple --tls-domain flags to serve multiple domains.
    The resulting files are cached in the "$HOME/.cache/chisel" directory.
    You can modify this path by setting the CHISEL_LE_CACHE variable,
    or disable caching by setting this variable to "-". You can optionally
    provide a certificate notification email by setting CHISEL_LE_EMAIL.

    --tls-ca, a path to a PEM encoded CA certificate bundle or a directory
    holding multiple PEM encode CA certificate bundle files, which is used to 
    validate client connections. The provided CA certificates will be used 
    instead of the system roots. This is commonly used to implement mutual-TLS. 

    --pid Generate pid file in current working directory

    -v, Enable verbose logging

    --help, This help text

  Signals:
    The chisel process is listening for:
      a SIGINT or SIGTERM to begin a graceful shutdown
        (a second signal forces an immediate exit),
      a SIGUSR2 to print process stats, and
      a SIGHUP to short-circuit the client reconnect timer

  Version:
    X.Y.Z

  Read more:
    https://github.com/jpillora/chisel
$ chisel client --help

  Usage: chisel client [options] <server> <remote> [remote] [remote] ...

  <server> is the URL to the chisel server.

  <remote>s are remote connections tunneled through the server, each of
  which come in the form:

    <local-host>:<local-port>:<remote-host>:<remote-port>/<protocol>

    ■ local-host defaults to 0.0.0.0 (all interfaces).
    ■ local-port defaults to remote-port.
    ■ remote-port is required*.
    ■ remote-host defaults to 127.0.0.1 (server localhost).
    ■ protocol defaults to tcp.

  which shares <remote-host>:<remote-port> from the server to the client
  as <local-host>:<local-port>, or:

    R:<local-interface>:<local-port>:<remote-host>:<remote-port>/<protocol>

  which does reverse port forwarding, sharing <remote-host>:<remote-port>
  from the client to the server's <local-interface>:<local-port>.

    example remotes

      3000
      example.com:3000
      3000:google.com:80
      192.168.0.5:3000:google.com:80
      socks
      5000:socks
      R:2222:localhost:22
      R:socks
      R:5000:socks
      stdio:example.com:22
      1.1.1.1:53/udp

    When the chisel server has --socks5 enabled, remotes can
    specify "socks" in place of remote-host and remote-port.
    The default local host and port for a "socks" remote is
    127.0.0.1:1080. Connections to this remote will terminate
    at the server's internal SOCKS5 proxy. When the server also
    has --authfile set, SOCKS5 access requires an entry matching
    the token "socks" in the user's address list.

    When the chisel server has --reverse enabled, remotes can
    be prefixed with R to denote that they are reversed. That
    is, the server will listen and accept connections, and they
    will be proxied through the client which specified the remote.
    Reverse remotes specifying "R:socks" will listen on the server's
    default socks port (1080) and terminate the connection at the
    client's internal SOCKS5 proxy.

    When stdio is used as local-host, the tunnel will connect standard
    input/output of this program with the remote. This is useful when 
    combined with ssh ProxyCommand. You can use
      ssh -o ProxyCommand='chisel client chiselserver stdio:%h:%p' \
          [email protected]
    to connect to an SSH server through the tunnel.

  Options:

    --fingerprint, A *strongly recommended* fingerprint string
    to perform host-key validation against the server's public key.
    Fingerprint mismatches will close the connection.
    Fingerprints are generated by hashing the ECDSA public key using
    SHA256 and encoding the result in base64.
    Fingerprints must be 44 characters containing a trailing equals (=).
    Legacy MD5 colon fingerprints (deprecated) are still accepted,
    but only in their full 16-octet form; truncated prefixes are
    rejected.

    --auth, An optional username and password (client authentication)
    in the form: "<user>:<pass>". These credentials are compared to
    the credentials inside the server's --authfile. defaults to the
    AUTH environment variable.

    --keepalive, An optional keepalive interval. Since the underlying
    transport is HTTP, in many instances we'll be traversing through
    proxies, often these proxies will close idle connections. You must
    specify a time with a unit, for example '5s' or '2m'. Defaults
    to '25s' (set to 0s to disable).

    --max-retry-count, Maximum number of times to retry before exiting.
    Defaults to unlimited.

    --min-retry-interval, Minimum wait time before retrying after a
    disconnection. Defaults to 1 second.

    --max-retry-interval, Maximum wait time before retrying after a
    disconnection. Defaults to 5 minutes.

    --proxy, An optional HTTP CONNECT or SOCKS5 proxy which will be
    used to reach the chisel server. Authentication can be specified
    inside the URL. Credentials must be URL-encoded; for example a
    "#" in the password must be written as "%23".
    For example, http://admin:[email protected]:8081
            or: socks://admin:[email protected]:1080
    The socks://, socks5:// and socks5h:// schemes are equivalent:
    DNS is always resolved by the proxy.

    --header, Set a custom header in the form "HeaderName: HeaderContent".
    Can be used multiple times. (e.g --header "Foo: Bar" --header "Hello: World")

    --hostname, Optionally set the 'Host' header (defaults to the host
    found in the server url).

    --sni, Override the ServerName when using TLS (defaults to the 
    hostname).

    --tls-ca, An optional root certificate bundle used to verify the
    chisel server. Only valid when connecting to the server with
    "https" or "wss". By default, the operating system CAs will be used.

    --tls-skip-verify, Skip server TLS certificate verification of
    chain and host name (if TLS is used for transport connections to
    server). If set, client accepts any TLS certificate presented by
    the server and any host name in that certificate. This only affects
    transport https (wss) connection. Chisel server's public key
    may be still verified (see --fingerprint) after inner connection
    is established.

    --tls-key, a path to a PEM encoded private key used for client 
    authentication (mutual-TLS).

    --tls-cert, a path to a PEM encoded certificate matching the provided 
    private key. The certificate must have client authentication 
    enabled (mutual-TLS).

    --pid Generate pid file in current working directory

    -v, Enable verbose logging

    --help, This help text

  Signals:
    The chisel process is listening for:
      a SIGINT or SIGTERM to begin a graceful shutdown
        (a second signal forces an immediate exit),
      a SIGUSR2 to print process stats, and
      a SIGHUP to short-circuit the client reconnect timer

  Version:
    X.Y.Z

  Read more:
    https://github.com/jpillora/chisel

Security

Encryption is always enabled. When you start up a chisel server, it will generate an in-memory ECDSA public/private key pair. The public key fingerprint (base64 encoded SHA256) will be displayed as the server starts. Instead of generating a random key, the server may optionally specify a key file, using the --keyfile option. When clients connect, they will also display the server's public key fingerprint. The client can force a particular fingerprint using the --fingerprint option. Legacy MD5 fingerprints are still accepted but must be the full 16-octet colon form — truncated prefixes are rejected. See the --help above for more information.

The server also caps inbound websocket message sizes before authentication (CHISEL_WS_READ_LIMIT, default 512 KiB), so unauthenticated peers cannot exhaust memory with oversized messages. The default sits comfortably above x/crypto/ssh's 256 KiB maximum transport packet, so no valid SSH packet is ever rejected. Only 0 disables the limit; negative values fall back to the safe default.

Authentication

Using the --authfile option, the server may optionally provide a user.json configuration file to create a list of accepted users. The client then authenticates using the --auth option. See users.json for an example authentication configuration file. See the --help above for more information.

Notes on authfile behavior:

Internally, this is done using the Password authentication method provided by SSH. Learn more about crypto/ssh here http://blog.gopheracademy.com/go-and-ssh/. Session opens/closes (with user, source address and remotes) and failed login attempts are logged at info level.

TLS Guide

The simplest secure setup is --tls-domain, which provisions a LetsEncrypt certificate automatically (requires port 443 and a DNS record pointing at the server):

chisel server --port 443 --tls-domain chisel.example.com --auth user:pass
chisel client --auth user:pass https://chisel.example.com R:2222:localhost:22

To use your own certificate (self-signed or internal CA), generate a key/cert pair and point both sides at the right files:

chisel server --port 443 --tls-key key.pem --tls-cert cert.pem
chisel client --tls-ca ca.pem https://chisel.example.com 3000

For mutual TLS, also pass --tls-ca to the server and --tls-cert/--tls-key to each client. Note that TLS wraps chisel's transport from the outside; the inner SSH layer still encrypts and authenticates, so --fingerprint validation works with or without TLS.

SOCKS5 Guide with Docker

  1. Print a new private key to the terminal

    chisel server --keygen -
    # or save it to disk --keygen /path/to/mykey
    
  2. Start your chisel server

    jpillora/chisel server --keyfile '<ck-base64 string or file path>' -p 9312 --socks5
    
  3. Connect your chisel client (using server's fingerprint)

    chisel client --fingerprint '<see server output>' <server-address>:9312 socks
    
  4. Point your SOCKS5 clients (e.g. OS/Browser) to:

    <client-address>:1080
    
  5. Now you have an encrypted, authenticated SOCKS5 connection over HTTP

Note: if the server also uses --authfile, users need an entry matching the token socks to use the proxy (see Authentication).

Reverse SOCKS with an Authfile

To let a specific client act as a SOCKS exit node, grant it the reverse-socks listener address (R:socks listens on the server's 127.0.0.1:1080):

{
  "exituser:password": ["^R:127\\.0\\.0\\.1:1080$"]
}
chisel server --reverse --authfile users.json
chisel client --auth exituser:password <server-address> R:socks
# server-side consumers point SOCKS5 clients at 127.0.0.1:1080,
# and their traffic exits via the chisel client's network

See also the step-by-step reverse tunneling example.

Running behind a CDN (Cloudflare)

chisel works through CDNs that support WebSockets. For Cloudflare: enable WebSockets, proxy (orange-cloud) the DNS record, and connect clients with https://. The CDN terminates TLS, but the inner SSH layer means --fingerprint validation still authenticates your chisel server end-to-end — the CDN cannot read or modify tunneled traffic. Keep --keepalive at its 25s default to stay under CDN idle timeouts, and note that proxies which strip Upgrade headers cannot carry chisel at all.

Tuning with environment variables

Less common knobs are environment variables, all read with a CHISEL_ prefix (e.g. CHISEL_WS_TIMEOUT=10s):

Variable Side Default Purpose
WS_TIMEOUT client 45s websocket handshake timeout
SSH_TIMEOUT client 30s ssh handshake timeout
CONFIG_TIMEOUT server 10s wait for the client's config request
SSH_WAIT both 35s how long new tunnels wait for an active connection
PING_TIMEOUT both keepalive interval keepalive ping reply timeout (no pings if --keepalive 0)
DIAL_TIMEOUT exit node 30s tcp dial timeout for tunnel targets
WS_READ_LIMIT both 524288 max inbound websocket message bytes (0 = no limit; negative = default)
WS_BUFF_SIZE both go default websocket read/write buffer sizes
UDP_MAX_SIZE both 9012 max udp packet bytes
UDP_DEADLINE both 15s udp flow read deadline and return-peer expiry
UDP_MAX_CONNS both 100 max udp flows or return peers per tunnel
SHUTDOWN_GRACE server 5s http request drain time on shutdown

HOST, PORT, AUTH, and CHISEL_KEY/CHISEL_KEY_FILE are documented in the --help texts above.

Caveats

Since WebSockets support is required:

Contributing

Changelog

Upgrading to 1.12

Four changes may require action when upgrading from 1.11.x or earlier:

  1. SOCKS5 + --authfile (enforced since v1.11.7): users who should keep proxy access need an authfile entry matching the token socks (the wildcard "" keeps working). See Authentication. Denied requests are logged server-side as Denied connection to socks (ACL).
  2. --fingerprint: truncated legacy MD5 fingerprints are rejected. Use the full SHA256 fingerprint printed by the server and client (the full 16-octet MD5 colon form is still accepted, but deprecated).
  3. --auth values must be <user>:<pass> — strings without a colon now fail at startup instead of silently disabling authentication.
  4. Exit codes: chisel client with --max-retry-count now exits non-zero when connection attempts are exhausted; scripts checking $? and systemd Restart=on-failure units will notice.

License

MIT © Jaime Pillora

chisel (jpillora/chisel)

Searching Database…

16.7K ·GitHub
这是做什么的?Chisel 是一个基于 HTTP 传输、通过 SSH 加密的快速 TCP/UDP 隧道工具——只有一个可执行文件,既可以当客户端也可以当服务端,主要用来穿透防火墙,或者给你的网络提供一个安全的接入入口。
它是单个程序,还是需要分别安装客户端和服务端?只有一个程序:单个 Go 可执行文件同时包含客户端和服务端,不需要安装任何运行时依赖。
最简单的安装方法是什么?从发布页面下载预编译的二进制文件(有 curl | bash 一键安装脚本),也可以从 Docker Hub 或 ghcr.io 拉取多架构 Docker 镜像,或者使用 Fedora 的 RPM 包;发布时也会附带 .deb 和 .apk 安装包。
部署时有什么要注意的坑?必须支持 WebSocket。各类 IaaS 服务商都没问题,PaaS 则情况不一(Google App Engine 标准环境不支持),会剥离 Upgrade 头的 HTTP 代理则完全无法使用 chisel。
它支持哪些操作系统?当前的构建版本至少需要 Windows 10/Server 2016、macOS 12、Linux 内核 3.2 或 FreeBSD 12.2;更老的系统(比如 Windows 7)请使用 v1.8.1 或更早的版本。
如果连接断了或者我的电脑休眠了会怎样?客户端会自动重连,采用指数退避策略(可通过 --min/max-retry-interval 调整),并且保活心跳超时后会检测到已经悄悄断开的连接并重新建立。
在 Cloudflare 或其他 CDN 后面能用吗?可以,任何支持 WebSocket 的 CDN 都行:开启 WebSocket,并把 DNS 记录设为橙色云(开启代理)。由于内层是 SSH,--fingerprint 仍能端到端验证服务端身份,CDN 无法读取或篡改隧道里的流量;--keepalive 保持默认的 25 秒即可。
它能转发 UDP 吗,还是只能转发 TCP?都可以。默认情况下 UDP 数据包上限为 9012 字节,每条隧道最多 100 个并发流,可以通过 CHISELUDPMAXSIZE 和 CHISELUDPMAXCONNS 环境变量调整。
不租服务器能试用吗?可以——example/fly.toml 能把 chisel 服务端部署到 fly.io 的免费额度上:在 example/ 目录下运行 fly launch --copy-config,然后就可以连接到与它一起运行的任意服务。
Creator
jpillora
Size
3.6 MB
Stars
16.7K
Age
11y
Commits · 90 Days