-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f7e9db
commit d093019
Showing
15 changed files
with
1,290 additions
and
9 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
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
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,27 @@ | ||
//go:build !windows | ||
|
||
package agent | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/crypto/ssh" | ||
gosshagent "golang.org/x/crypto/ssh/agent" | ||
) | ||
|
||
func GetSSHAuthSocket() string { | ||
sshAuthSocket := os.Getenv("SSH_AUTH_SOCK") | ||
if sshAuthSocket != "" { | ||
return sshAuthSocket | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func ForwardToRemote(client *ssh.Client, addr string) error { | ||
return gosshagent.ForwardToRemote(client, addr) | ||
} | ||
|
||
func RequestAgentForwarding(session *ssh.Session) error { | ||
return gosshagent.RequestAgentForwarding(session) | ||
} |
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,80 @@ | ||
package agent | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
"golang.org/x/crypto/ssh" | ||
gosshagent "golang.org/x/crypto/ssh/agent" | ||
"gopkg.in/natefinch/npipe.v2" | ||
) | ||
|
||
const ( | ||
channelType = "[email protected]" | ||
defaultNamedPipe = "\\\\.\\pipe\\openssh-ssh-agent" | ||
) | ||
|
||
func GetSSHAuthSocket() string { | ||
sshAuthSocket := os.Getenv("SSH_AUTH_SOCK") | ||
if sshAuthSocket != "" { | ||
return sshAuthSocket | ||
} | ||
if _, err := os.Stat(defaultNamedPipe); err == nil { | ||
return defaultNamedPipe | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func ForwardToRemote(client *ssh.Client, addr string) error { | ||
channels := client.HandleChannelOpen(channelType) | ||
if channels == nil { | ||
return errors.New("agent: already have handler for " + channelType) | ||
} | ||
conn, err := npipe.Dial(addr) | ||
if err != nil { | ||
return err | ||
} | ||
conn.Close() | ||
|
||
go func() { | ||
for ch := range channels { | ||
channel, reqs, err := ch.Accept() | ||
if err != nil { | ||
continue | ||
} | ||
go ssh.DiscardRequests(reqs) | ||
go forwardNamedPipe(channel, addr) | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
func RequestAgentForwarding(session *ssh.Session) error { | ||
return gosshagent.RequestAgentForwarding(session) | ||
} | ||
|
||
func forwardNamedPipe(channel ssh.Channel, addr string) { | ||
conn, err := npipe.Dial(addr) | ||
if err != nil { | ||
return | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
go func() { | ||
io.Copy(conn, channel) | ||
wg.Done() | ||
}() | ||
go func() { | ||
io.Copy(channel, conn) | ||
channel.CloseWrite() | ||
wg.Done() | ||
}() | ||
|
||
wg.Wait() | ||
conn.Close() | ||
channel.Close() | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.