github.com/openziti/transport@v0.1.5/transwarptls/listener.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 transwarptls 18 19 import ( 20 "github.com/michaelquigley/pfxlog" 21 "github.com/openziti/dilithium/cf" 22 "github.com/openziti/dilithium/protocol/westlsworld3" 23 "github.com/openziti/dilithium/protocol/westworld3" 24 "github.com/openziti/foundation/identity/identity" 25 "github.com/openziti/transport" 26 "github.com/pkg/errors" 27 "github.com/sirupsen/logrus" 28 "io" 29 "net" 30 ) 31 32 func Listen(bind *net.UDPAddr, name string, id *identity.TokenId, incoming chan transport.Connection, tcfg transport.Configuration) (io.Closer, error) { 33 log := pfxlog.ContextLogger(name + "/transwarptls:" + bind.String()) 34 35 profileId := byte(0) 36 if tcfg != nil { 37 profile := westworld3.NewBaselineProfile() 38 if err := profile.Load(cf.MapIToMapS(tcfg)); err != nil { 39 return nil, errors.Wrap(err, "load profile") 40 } 41 newProfileId, err := westworld3.AddProfile(profile) 42 if err != nil { 43 return nil, errors.Wrap(err, "register profile") 44 } 45 profileId = newProfileId 46 } 47 logrus.Infof("westworld3 profile = [\n%s\n]", westworld3.GetProfile(profileId).Dump()) 48 49 listener, err := westlsworld3.Listen(bind, id.ServerTLSConfig(), profileId) 50 if err != nil { 51 return nil, err 52 } 53 54 go acceptLoop(log.Entry, name, listener, incoming) 55 56 return listener, nil 57 } 58 59 func acceptLoop(log *logrus.Entry, name string, listener net.Listener, incoming chan transport.Connection) { 60 defer log.Error("exited") 61 62 for { 63 socket, err := listener.Accept() 64 if err != nil { 65 if netErr, ok := err.(net.Error); ok && !netErr.Temporary() { 66 log.WithField("err", err).Error("accept failed. failure not recoverable. exiting listen loop") 67 return 68 } 69 log.WithField("err", err).Error("accept failed") 70 71 } else { 72 connection := &Connection{ 73 detail: &transport.ConnectionDetail{ 74 Address: "transwarptls:" + socket.RemoteAddr().String(), 75 InBound: true, 76 Name: name, 77 }, 78 socket: socket, 79 } 80 incoming <- connection 81 82 log.WithField("addr", socket.RemoteAddr().String()).Info("accepted connection") 83 } 84 } 85 }