This project is mainly generated by large language models. It really speeds up the writing process. Of course, there is still some manual intervention to solve certain issues. This instruction document is also generated by large language models. To be honest, it’s better than what I could write myself.
The main function of this Go program is to implement a local proxy that can forward the traffic of a local port to a remote server through an SSH tunnel. The program will read the relevant configuration information from the configuration file forward_config.toml and load the necessary configuration for the SSH connection from the user’s SSH configuration file ~/.ssh/config. Finally, it will start the proxy service.
Make sure that the Go language is installed on your system and the Go version meets the requirements of the program.
This program depends on some third - party libraries. You need to use the following commands to install them:
go get github.com/kevinburke/ssh_config
go get golang.org/x/crypto/ssh
go get github.com/BurntSushi/tomlThe program will read relevant configuration information, such as the username, hostname, port number, and the path of the private key file, from the user’s SSH configuration file ~/.ssh/config. Make sure that this file exists and is configured correctly.
The program will read the forward_config.toml file to obtain the relevant configuration information for forwarding. The content format of this file is as follows:
remote_host = "example.com" # Hostname of the remote server
remote_port = "8080" # Port number of the remote server
local_port = "8081" # Local listening port number
ssh_host = "ssh.example.com" # Hostname of the SSH server
timeout = 30 # Connection timeout (in seconds)Make sure that the correct IdentityFile, i.e., the path of the SSH private key file, is configured for ssh_host in ~/.ssh/config.
If the private key file is password - protected, you need to set the environment variable SSH_PASSCODE to the password of the private key. For example:
export SSH_PASSCODE="your_passphrase"In the terminal, navigate to the directory where the program is located and use the following command to compile the program:
go build -o ssh_forward main.goThis will generate an executable file named ssh_forward.
After compilation, use the following command to run the program:
./ssh_forwardIf all configurations are correct, the program will start and output relevant information about the proxy service, for example: Forwarding proxy started on localhost:8081 -> example.com:8080 via ssh.example.com
The program uses ssh.InsecureIgnoreHostKey() to ignore SSH host key verification. This can be used in a test environment, but it is not recommended in a production environment because of security risks.
If an error occurs during the program’s execution, the corresponding error information will be output in the terminal. You can troubleshoot and fix the issue based on the error information.
Through the above steps, you can use this program to forward the traffic from a local port to a remote server.