//BaseProtocol returns a new protocol instance func BaseProtocol(desc *ProtocolDescriptor, rc *RouteConfig) *Protocol { return &Protocol{ NewBase(desc), make(chan struct{}), NewSessionManager(), rc, NewRoute(desc.Service, rc.buffer, rc.timeout, rc.fail), flux.PushSocket(0), flux.PushSocket(0), flux.PushSocket(0), } }
//FromRoute returns a route based on a previous route func FromRoute(r *Route, path string) *Route { return RawRoute(path, flux.DoPushSocket(r.Valid, func(v interface{}, s flux.SocketInterface) { req, ok := v.(*Request) if !ok { return } nreq := FromRequest(req, nil) s.Emit(nreq) }), flux.PushSocket(r.DTO), r.DTO, r.fail) }
//NewRoute returns a route struct for a specific route path // which allows us to do NewRoute('{id:[/d+/]}') //note when using long paths eg /apple/dog/back //this returns the root path 'apple' and not the final path 'back' route object func NewRoute(path string, buf int, ts int, fail Failure) *Route { vs := splitPatternAndRemovePrefix(path) if len(vs) <= 0 { panic(fmt.Sprintf("path after split is is %v not valid", vs)) } fs := vs[0] if len(vs) > 1 { vs = vs[1:] } else { vs = vs[0:0] } rw := RawRoute(fs, flux.PushSocket(buf), flux.PushSocket(buf), ts, fail) if len(vs) > 0 { rw.New(strings.Join(vs, "/")) } return rw }
//PasswordSSHProtocol creates a ssh-server that handles ssh-connections func PasswordSSHProtocol(rc *RouteConfig, service, addr string, port int, rsaFile string, auth PassAuth) *SSHProtocol { pbytes, err := ioutil.ReadFile(rsaFile) if err != nil { panic(fmt.Sprintf("ReadError %v \nFailed to load private key file: %s", err, rsaFile)) } private, err := ssh.ParsePrivateKey(pbytes) if err != nil { panic(fmt.Sprintf("ParseError:(%s): \n %s -> %v", rsaFile, "Failed to parse private key!", err)) } desc := NewDescriptor("ssh", service, addr, port, "0", "ssh") conf := &ssh.ServerConfig{ PasswordCallback: auth, } conf.AddHostKey(private) sd := &SSHProtocol{ BaseProtocol(desc, rc), flux.PushSocket(0), flux.PushSocket(0), flux.PushSocket(0), conf, nil, nil, nil, } setupServer(sd) return sd }
//InvertRoute returns a route based on a previous route rejection of a request //provides a divert like or not path logic (i.e if that path does not match the parent route) //then this gets validate that rejected route) //the fail action can be set as nil which then uses the previous fail action from //the previous route func InvertRoute(r *Route, path string, fail Failure) *Route { valids := flux.DoPushSocket(r.Invalid, func(v interface{}, s flux.SocketInterface) { req, ok := v.(*Request) if !ok { return } s.Emit(req) }) if fail == nil { fail = r.fail } return RawRoute(path, valids, flux.PushSocket(r.DTO), r.DTO, fail) }