12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package sshprox
- import (
- "fmt"
- "net"
- "net/url"
- "strings"
- "time"
- )
- //Rewrite url based on proxy root
- func RewriteURL(rooturl string, requestURL string) (*url.URL, error) {
- rewrittenURL := strings.TrimPrefix(requestURL, rooturl)
- return url.Parse(rewrittenURL)
- }
- //Check if a given domain and port is a valid ssh server
- func IsSSHConnectable(ipOrDomain string, port int) bool {
- timeout := time.Second * 3
- conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ipOrDomain, port), timeout)
- if err != nil {
- return false
- }
- defer conn.Close()
- // Send an SSH version identification string to the server to check if it's SSH
- _, err = conn.Write([]byte("SSH-2.0-Go\r\n"))
- if err != nil {
- return false
- }
- // Wait for a response from the server
- buf := make([]byte, 1024)
- _, err = conn.Read(buf)
- if err != nil {
- return false
- }
- // Check if the response starts with "SSH-2.0"
- return string(buf[:7]) == "SSH-2.0"
- }
- //Check if the port is used by other process or application
- func isPortInUse(port int) bool {
- address := fmt.Sprintf(":%d", port)
- listener, err := net.Listen("tcp", address)
- if err != nil {
- return true
- }
- listener.Close()
- return false
- }
|