Monorepo for Tangled tangled.org
2

Configure Feed

Select the types of activity you want to include in your feed.

at master 1.8 kB View raw
1package hostutil 2 3import ( 4 "fmt" 5 "net/url" 6 "strings" 7 8 "github.com/bluesky-social/indigo/atproto/syntax" 9) 10 11func ParseHostname(raw string) (hostname string, noSSL bool, err error) { 12 // handle case of bare hostname 13 if !strings.Contains(raw, "://") { 14 if strings.HasPrefix(raw, "localhost:") { 15 raw = "http://" + raw 16 } else { 17 raw = "https://" + raw 18 } 19 } 20 21 u, err := url.Parse(raw) 22 if err != nil { 23 return "", false, fmt.Errorf("not a valid host URL: %w", err) 24 } 25 26 switch u.Scheme { 27 case "https", "wss": 28 noSSL = false 29 case "http", "ws": 30 noSSL = true 31 default: 32 return "", false, fmt.Errorf("unsupported URL scheme: %s", u.Scheme) 33 } 34 35 // 'localhost' (exact string) is allowed *with* a required port number; SSL is optional 36 if u.Hostname() == "localhost" { 37 if u.Port() == "" || !strings.HasPrefix(u.Host, "localhost:") { 38 return "", false, fmt.Errorf("port number is required for localhost") 39 } 40 return u.Host, noSSL, nil 41 } 42 43 // port numbers not allowed otherwise 44 if u.Port() != "" { 45 return "", false, fmt.Errorf("port number not allowed for non-local names") 46 } 47 48 // check it is a real hostname (eg, not IP address or single-word alias) 49 h, err := syntax.ParseHandle(u.Host) 50 if err != nil { 51 return "", false, fmt.Errorf("not a public hostname") 52 } 53 54 // lower-case in response 55 return h.Normalize().String(), noSSL, nil 56} 57 58func EnsureHttpScheme(host string) (string, error) { 59 hostname, noSSL, err := ParseHostname(host) 60 if err != nil { 61 return "", err 62 } 63 if noSSL { 64 return "http://" + hostname, nil 65 } else { 66 return "https://" + hostname, nil 67 } 68} 69 70func EnsureWsScheme(host string) (string, error) { 71 hostname, noSSL, err := ParseHostname(host) 72 if err != nil { 73 return "", err 74 } 75 if noSSL { 76 return "ws://" + hostname, nil 77 } else { 78 return "wss://" + hostname, nil 79 } 80}