123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package sshprox
- import (
- "errors"
- "fmt"
- "net"
- "net/url"
- "regexp"
- "runtime"
- "strings"
- "time"
- )
- func RewriteURL(rooturl string, requestURL string) (*url.URL, error) {
- rewrittenURL := strings.TrimPrefix(requestURL, rooturl)
- return url.Parse(rewrittenURL)
- }
- func IsWebSSHSupported() bool {
-
- binary := "gotty_" + runtime.GOOS + "_" + runtime.GOARCH
- if runtime.GOOS == "windows" {
- binary = binary + ".exe"
- }
-
- f, err := gotty.Open("gotty/" + binary)
- if err != nil {
- return false
- }
- f.Close()
- return true
- }
- func (m *Manager) GetNextPort() int {
- nextPort := m.StartingPort
- occupiedPort := make(map[int]bool)
- for _, instance := range m.Instances {
- occupiedPort[instance.AssignedPort] = true
- }
- for {
- if !occupiedPort[nextPort] {
- return nextPort
- }
- nextPort++
- }
- }
- 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()
-
- _, err = conn.Write([]byte("SSH-2.0-Go\r\n"))
- if err != nil {
- return false
- }
-
- buf := make([]byte, 1024)
- _, err = conn.Read(buf)
- if err != nil {
- return false
- }
-
- return string(buf[:7]) == "SSH-2.0"
- }
- func ValidateUsernameAndRemoteAddr(username string, remoteIpAddr string) error {
-
- validUsername := regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
- if !validUsername.MatchString(username) {
- return errors.New("invalid username, only alphanumeric characters, dots, underscores and dashes are allowed")
- }
-
- if net.ParseIP(remoteIpAddr) != nil {
-
- return nil
- }
-
- validRemoteAddr := regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
- if !validRemoteAddr.MatchString(remoteIpAddr) {
- return errors.New("invalid remote address, only alphanumeric characters, dots, underscores and dashes are allowed")
- }
- return nil
- }
- func IsLoopbackIPOrDomain(ipOrDomain string) bool {
- if strings.EqualFold(strings.TrimSpace(ipOrDomain), "localhost") || strings.TrimSpace(ipOrDomain) == "127.0.0.1" {
- return true
- }
-
- ips, err := net.LookupIP(ipOrDomain)
- if err != nil {
- return false
- }
- for _, ip := range ips {
- if ip.IsLoopback() {
- return true
- }
- }
- return false
- }
|