-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minitunnel] track tunneled ports for listing and closing (#1509)
* [minitunnel] track tunneled ports for listing and closing * Skip logging of expected errors when users delete a tunnel * Add small blurb in help for "cc tunnel" command * Use VM name when listing tunnels in case multiple VMs have same hostname
- Loading branch information
1 parent
4e36236
commit 2b2b341
Showing
4 changed files
with
255 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package minitunnel | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
type forward struct { | ||
fid int | ||
src int | ||
host string | ||
dst int | ||
|
||
listener net.Listener | ||
connections []net.Conn | ||
} | ||
|
||
func (f *forward) addConnection(c net.Conn) { | ||
f.connections = append(f.connections, c) | ||
} | ||
|
||
func (f *forward) close() { | ||
f.listener.Close() | ||
|
||
for _, conn := range f.connections { | ||
conn.Close() | ||
} | ||
} | ||
|
||
func (f *forward) String() string { | ||
return fmt.Sprintf("%d:%s:%d", f.src, f.host, f.dst) | ||
} | ||
|
||
func (t *Tunnel) newForward(l net.Listener, src int, host string, dst int) *forward { | ||
return &forward{ | ||
fid: <-t.forwardIDs, | ||
src: src, | ||
host: host, | ||
dst: dst, | ||
|
||
listener: l, | ||
} | ||
} | ||
|
||
func (t *Tunnel) ListForwards() map[int]string { | ||
list := make(map[int]string) | ||
|
||
t.sendLock.Lock() | ||
defer t.sendLock.Unlock() | ||
|
||
for i, f := range t.forwards { | ||
list[i] = f.String() | ||
} | ||
|
||
return list | ||
} | ||
|
||
func (t *Tunnel) CloseForward(id int) error { | ||
f, ok := t.forwards[id] | ||
if !ok { | ||
return fmt.Errorf("forwarder with ID %d not found", id) | ||
} | ||
|
||
f.close() | ||
|
||
t.sendLock.Lock() | ||
defer t.sendLock.Unlock() | ||
|
||
delete(t.forwards, f.fid) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters