github.com/openziti/transport@v0.1.5/wss/address.go (about) 1 /* 2 Copyright NetFoundry, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 https://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package wss 18 19 import ( 20 "errors" 21 "fmt" 22 "github.com/openziti/foundation/identity/identity" 23 "github.com/openziti/transport" 24 "io" 25 "strconv" 26 "strings" 27 "time" 28 ) 29 30 var _ transport.Address = (*address)(nil) // enforce that address implements transport.Address 31 32 type address struct { 33 hostname string 34 port uint16 35 } 36 37 func (a address) Dial(name string, i *identity.TokenId, _ time.Duration, _ transport.Configuration) (transport.Connection, error) { 38 return Dial(a.bindableAddress(), name) 39 } 40 41 func (a address) DialWithLocalBinding(name string, localBinding string, _ *identity.TokenId, timeout time.Duration, _ transport.Configuration) (transport.Connection, error) { 42 return DialWithLocalBinding(a.bindableAddress(), name, localBinding) 43 } 44 45 func (a address) Listen(name string, i *identity.TokenId, incoming chan transport.Connection, tcfg transport.Configuration) (io.Closer, error) { 46 var subc map[interface{}]interface{} 47 if tcfg != nil { 48 if v, found := tcfg["wss"]; found { 49 if subv, ok := v.(map[interface{}]interface{}); ok { 50 subc = subv 51 } 52 } 53 } 54 55 return Listen(a.bindableAddress(), name, incoming, subc) 56 } 57 58 func (a address) MustListen(name string, i *identity.TokenId, incoming chan transport.Connection, tcfg transport.Configuration) io.Closer { 59 closer, err := a.Listen(name, i, incoming, tcfg) 60 if err != nil { 61 panic(err) 62 } 63 return closer 64 } 65 66 func (a address) String() string { 67 return fmt.Sprintf("wss:%s", a.bindableAddress()) 68 } 69 70 func (a address) bindableAddress() string { 71 return fmt.Sprintf("%s:%d", a.hostname, a.port) 72 } 73 74 func (a address) Type() string { 75 return "wss" 76 } 77 78 type AddressParser struct{} 79 80 func (ap AddressParser) Parse(s string) (transport.Address, error) { 81 tokens := strings.Split(s, ":") 82 if len(tokens) < 2 { 83 return nil, errors.New("invalid format") 84 } 85 86 if tokens[0] == "wss" { 87 if len(tokens) != 3 { 88 return nil, errors.New("invalid format") 89 } 90 91 port, err := strconv.ParseUint(tokens[2], 10, 16) 92 if err != nil { 93 return nil, err 94 } 95 96 return &address{hostname: tokens[1], port: uint16(port)}, nil 97 } 98 99 return nil, errors.New("invalid format") 100 }