Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/release-binaries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Release Binaries
on:
release:
types: [published]
permissions:
contents: write
jobs:
build:
name: ${{ matrix.goos }}/${{ matrix.goarch }}${{ matrix.goarm && format('/arm{0}', matrix.goarm) || '' }}
runs-on: ubuntu-latest
strategy:
matrix:
include:
- {goos: linux, goarch: amd64}
- {goos: linux, goarch: arm64}
- {goos: linux, goarch: arm, goarm: "7"}
- {goos: linux, goarch: arm, goarm: "6"}
- {goos: linux, goarch: "386"}
- {goos: darwin, goarch: amd64}
- {goos: darwin, goarch: arm64}
- {goos: windows, goarch: amd64}
- {goos: windows, goarch: arm64}
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: stable
- name: Build
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
GOARM: ${{ matrix.goarm }}
CGO_ENABLED: "0"
run: |
SUFFIX="${{ matrix.goarm && format('v{0}', matrix.goarm) || '' }}"
EXT="${{ matrix.goos == 'windows' && '.exe' || '' }}"
NAME="chisel_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }}${SUFFIX}${EXT}"
go build -trimpath \
-ldflags="-s -w -X github.com/jpillora/chisel/share.BuildVersion=${{ github.ref_name }}" \
-o "$NAME" .
echo "ASSET=$NAME" >> $GITHUB_ENV
- name: Upload
uses: softprops/action-gh-release@v2
with:
files: ${{ env.ASSET }}
30 changes: 24 additions & 6 deletions share/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,32 @@ func (t *Tunnel) keepAliveLoop(sshConn ssh.Conn) {
//ping forever
for {
time.Sleep(t.Config.KeepAlive)
_, b, err := sshConn.SendRequest("ping", true, nil)
if err != nil {
break
// SendRequest blocks indefinitely on a dead connection (e.g. after
// sleep/wake), so run it in a goroutine and treat no response within
// KeepAlive as a failure.
type result struct {
b []byte
err error
}
if len(b) > 0 && !bytes.Equal(b, []byte("pong")) {
t.Debugf("strange ping response")
break
ch := make(chan result, 1)
go func() {
_, b, err := sshConn.SendRequest("ping", true, nil)
ch <- result{b, err}
}()
select {
case r := <-ch:
if r.err != nil {
break
}
if len(r.b) > 0 && !bytes.Equal(r.b, []byte("pong")) {
t.Debugf("strange ping response")
break
}
continue
case <-time.After(t.Config.KeepAlive):
t.Debugf("ping timeout")
}
break
}
//close ssh connection on abnormal ping
sshConn.Close()
Expand Down
127 changes: 127 additions & 0 deletions share/tunnel/tunnel_keepalive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package tunnel

import (
"net"
"testing"
"time"

"github.com/jpillora/chisel/share/cio"
"golang.org/x/crypto/ssh"
)

func newTestTunnel(ka time.Duration) *Tunnel {
return New(Config{
Logger: cio.NewLogger("test"),
KeepAlive: ka,
})
}

// mockDeadSSHConn simulates an ssh.Conn whose SendRequest blocks indefinitely,
// as happens when the underlying TCP connection is dead but the OS has not yet
// detected it (e.g. immediately after a sleep/wake cycle with no RST received).
type mockDeadSSHConn struct {
closed chan struct{}
}

func (m *mockDeadSSHConn) User() string { return "" }
func (m *mockDeadSSHConn) SessionID() []byte { return nil }
func (m *mockDeadSSHConn) ClientVersion() []byte { return nil }
func (m *mockDeadSSHConn) ServerVersion() []byte { return nil }
func (m *mockDeadSSHConn) RemoteAddr() net.Addr { return &net.TCPAddr{} }
func (m *mockDeadSSHConn) LocalAddr() net.Addr { return &net.TCPAddr{} }
func (m *mockDeadSSHConn) OpenChannel(string, []byte) (ssh.Channel, <-chan *ssh.Request, error) {
return nil, nil, net.ErrClosed
}
func (m *mockDeadSSHConn) Wait() error { <-m.closed; return nil }
func (m *mockDeadSSHConn) Close() error {
select {
case <-m.closed:
default:
close(m.closed)
}
return nil
}

// SendRequest blocks until Close() is called, simulating a dead TCP connection.
func (m *mockDeadSSHConn) SendRequest(_ string, _ bool, _ []byte) (bool, []byte, error) {
<-m.closed
return false, nil, net.ErrClosed
}

// TestKeepAliveLoopTimeout verifies that keepAliveLoop calls sshConn.Close()
// when SendRequest does not return within the keepalive interval. This is the
// sleep/wake scenario where the TCP connection is silently dead.
func TestKeepAliveLoopTimeout(t *testing.T) {
const ka = 50 * time.Millisecond

mock := &mockDeadSSHConn{closed: make(chan struct{})}
tun := newTestTunnel(ka)

go tun.keepAliveLoop(mock)

select {
case <-mock.closed:
// keepAliveLoop detected the dead connection and called sshConn.Close()
case <-time.After(5 * ka):
t.Fatal("keepAliveLoop did not close dead connection within timeout (2×keepalive)")
}
}

// mockHealthySSHConn simulates a normal ssh.Conn that responds to pings immediately.
type mockHealthySSHConn struct {
closed chan struct{}
pingCount int
}

func (m *mockHealthySSHConn) User() string { return "" }
func (m *mockHealthySSHConn) SessionID() []byte { return nil }
func (m *mockHealthySSHConn) ClientVersion() []byte { return nil }
func (m *mockHealthySSHConn) ServerVersion() []byte { return nil }
func (m *mockHealthySSHConn) RemoteAddr() net.Addr { return &net.TCPAddr{} }
func (m *mockHealthySSHConn) LocalAddr() net.Addr { return &net.TCPAddr{} }
func (m *mockHealthySSHConn) OpenChannel(string, []byte) (ssh.Channel, <-chan *ssh.Request, error) {
return nil, nil, net.ErrClosed
}
func (m *mockHealthySSHConn) Wait() error { <-m.closed; return nil }
func (m *mockHealthySSHConn) Close() error {
select {
case <-m.closed:
default:
close(m.closed)
}
return nil
}
func (m *mockHealthySSHConn) SendRequest(_ string, _ bool, _ []byte) (bool, []byte, error) {
select {
case <-m.closed:
return false, nil, net.ErrClosed
default:
m.pingCount++
return true, []byte("pong"), nil
}
}

// TestKeepAliveLoopHealthy verifies that keepAliveLoop does NOT close the
// connection when the remote responds to pings normally.
func TestKeepAliveLoopHealthy(t *testing.T) {
const ka = 30 * time.Millisecond

mock := &mockHealthySSHConn{closed: make(chan struct{})}
tun := newTestTunnel(ka)

go tun.keepAliveLoop(mock)

// Let a few ping cycles pass.
time.Sleep(4 * ka)

select {
case <-mock.closed:
t.Fatal("keepAliveLoop closed a healthy connection unexpectedly")
default:
if mock.pingCount < 2 {
t.Fatalf("expected at least 2 pings, got %d", mock.pingCount)
}
}

mock.Close() // clean up
}
Loading