func getPass(r *http.Request) (user, pass string) { host, port, err := net.SplitHostPort(r.Host) if err != nil { host = r.Host if r.TLS != nil { port = "443" } else { port = "80" } } for _, h := range cfg.Host { host1, port1, _ := net.SplitHostPort(h.Host) if host1 == host && port == port1 { // host, port match return h.ProxyUser, h.ProxyPasswd } else if host1 == "" && port1 == port { // port match and host is wildcard return h.ProxyUser, h.ProxyPasswd } else if host1 == "0.0.0.0" && port == port1 { // port match and host is wildcard return h.ProxyUser, h.ProxyPasswd } else if host1 == "::" && port == port1 { // port match and host is wildcard return h.ProxyUser, h.ProxyPasswd } } return "", "" }
func (s *Server) getARecords(q dns.Question) (records []dns.RR, err error) { var h string name := strings.TrimSuffix(q.Name, ".") // Leader should always be listed if name == "leader."+s.domain || name == "master."+s.domain || name == s.domain { h, _, err = net.SplitHostPort(s.Leader()) if err != nil { return } records = append(records, &dns.A{Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 15}, A: net.ParseIP(h)}) } if name == s.domain { for _, m := range s.Members() { h, _, err = net.SplitHostPort(m) if err != nil { return } records = append(records, &dns.A{Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 15}, A: net.ParseIP(h)}) } } return }
// hostAddr returns the host and port that remote nodes will use to reach this // node. func (s *Server) hostAddr() (string, string, error) { // Resolve host to address. _, port, err := net.SplitHostPort(s.BindAddress) if err != nil { return "", "", fmt.Errorf("split bind address: %s", err) } host := s.Hostname // See if we might have a port that will override the BindAddress port if host != "" && host[len(host)-1] >= '0' && host[len(host)-1] <= '9' && strings.Contains(host, ":") { hostArg, portArg, err := net.SplitHostPort(s.Hostname) if err != nil { return "", "", err } if hostArg != "" { host = hostArg } if portArg != "" { port = portArg } } return host, port, nil }
// authorityAddr returns a given authority (a host/IP, or host:port / ip:port) // and returns a host:port. The port 443 is added if needed. func (t *Transport) authorityAddr(req *http.Request) (addr string) { //var err error h := req.URL.Host if h == "" { h = req.Host } /* no proxy */ if t.Proxy == nil { if _, _, err := net.SplitHostPort(h); err != nil { return net.JoinHostPort(h, "443") } return h } /* get proxy */ u, err := t.Proxy(req) if err != nil { if _, _, err := net.SplitHostPort(h); err != nil { return net.JoinHostPort(h, "443") } return h } if _, _, err := net.SplitHostPort(u.Host); err != nil { return net.JoinHostPort(u.Host, "443") } return u.Host }
func (upstream *Upstream) Validate() (valid bool) { var host, port string host, port, err := net.SplitHostPort(upstream.Server) if err != nil { server := upstream.Server + ":25565" if host, port, err = net.SplitHostPort(server); err != nil { log.Error("Invalid upstream server: " + upstream.Server) return false } log.Infof("no port information found in %s, assume 25565", upstream.Server) upstream.Server = server } p, err := strconv.ParseUint(port, 10, 16) if err != nil { log.Errorf("Invalid port %s: %s", port, err.Error()) return false } host = strings.ToLower(host) if !CheckHost(host) { log.Error("Invalid upstream host: " + host) return false } upstream.Server = net.JoinHostPort(host, fmt.Sprintf("%d", p)) upstream.Pattern = strings.ToLower(upstream.Pattern) if !CheckPattern(upstream.Pattern) { log.Error("Invalid pattern: " + upstream.Pattern) return false } if upstream.ErrorMsg.Text == "" { log.Warnf("Empty error text for %s, use default string", upstream.Server) upstream.ErrorMsg.Text = "Connection failed to " + upstream.Server } upstream.ChatMsg = ToChatMsg(&upstream.ErrorMsg) return true }
func (wr *Wrangler) validateReplication(shardInfo *topo.ShardInfo, tabletMap map[topo.TabletAlias]*topo.TabletInfo, results chan<- vresult) { _, ok := tabletMap[shardInfo.MasterAlias] if !ok { results <- vresult{shardInfo.MasterAlias.String(), fmt.Errorf("master not in tablet map")} return } actionPath, err := wr.ai.GetSlaves(shardInfo.MasterAlias) if err != nil { results <- vresult{shardInfo.MasterAlias.String(), err} return } sa, err := wr.ai.WaitForCompletionReply(actionPath, wr.actionTimeout()) if err != nil { results <- vresult{shardInfo.MasterAlias.String(), err} return } slaveAddrs := sa.(*tm.SlaveList).Addrs if len(slaveAddrs) == 0 { results <- vresult{shardInfo.MasterAlias.String(), fmt.Errorf("no slaves found")} return } // Some addresses don't resolve in all locations, just use IP address if err != nil { results <- vresult{shardInfo.MasterAlias.String(), fmt.Errorf("resolve slaves failed: %v", err)} return } tabletIpMap := make(map[string]*topo.Tablet) for _, tablet := range tabletMap { ipAddr, _, err := net.SplitHostPort(tablet.MysqlIpAddr) if err != nil { results <- vresult{tablet.Alias().String(), fmt.Errorf("bad mysql addr: %v %v", tablet.MysqlIpAddr, err)} continue } tabletIpMap[ipAddr] = tablet.Tablet } // See if every slave is in the replication graph. for _, slaveAddr := range slaveAddrs { if tabletIpMap[slaveAddr] == nil { results <- vresult{shardInfo.Keyspace() + "/" + shardInfo.ShardName(), fmt.Errorf("slave not in replication graph: %v (mysql instance without vttablet?)", slaveAddr)} } } // See if every entry in the replication graph is connected to the master. for _, tablet := range tabletMap { if !tablet.IsSlaveType() { continue } ipAddr, _, err := net.SplitHostPort(tablet.MysqlIpAddr) if err != nil { results <- vresult{tablet.Alias().String(), fmt.Errorf("bad mysql addr: %v", err)} } else if !strInList(slaveAddrs, ipAddr) { results <- vresult{tablet.Alias().String(), fmt.Errorf("slave not replicating: %v %q", ipAddr, slaveAddrs)} } } }
// Resolve an address string into a net.TCPAddr. We are a bit more strict than // net.ResolveTCPAddr; we don't allow an empty host or port, and the host part // must be a literal IP address. func resolveAddr(addrStr string) (*net.TCPAddr, error) { ipStr, portStr, err := net.SplitHostPort(addrStr) if err != nil { // Before the fixing of bug #7011, tor doesn't put brackets around IPv6 // addresses. Split after the last colon, assuming it is a port // separator, and try adding the brackets. parts := strings.Split(addrStr, ":") if len(parts) <= 2 { return nil, err } addrStr := "[" + strings.Join(parts[:len(parts)-1], ":") + "]:" + parts[len(parts)-1] ipStr, portStr, err = net.SplitHostPort(addrStr) } if err != nil { return nil, err } if ipStr == "" { return nil, net.InvalidAddrError(fmt.Sprintf("address string %q lacks a host part", addrStr)) } if portStr == "" { return nil, net.InvalidAddrError(fmt.Sprintf("address string %q lacks a port part", addrStr)) } ip := net.ParseIP(ipStr) if ip == nil { return nil, net.InvalidAddrError(fmt.Sprintf("not an IP string: %q", ipStr)) } port, err := parsePort(portStr) if err != nil { return nil, err } return &net.TCPAddr{IP: ip, Port: port}, nil }
func (s *Ethereum) ConnectToPeer(addr string) error { if s.peers.Len() < s.MaxPeers { var alreadyConnected bool eachPeer(s.peers, func(p *Peer, v *list.Element) { if p.conn == nil { return } phost, _, _ := net.SplitHostPort(p.conn.RemoteAddr().String()) ahost, _, _ := net.SplitHostPort(addr) if phost == ahost { alreadyConnected = true return } }) if alreadyConnected { return nil } peer := NewOutboundPeer(addr, s, s.serverCaps) s.peers.PushBack(peer) log.Printf("[SERV] Adding peer %d / %d\n", s.peers.Len(), s.MaxPeers) } return nil }
func (lp *Peers) Know(peer, id string) bool { lp.Lock() defer lp.Unlock() for _, p := range lp.peerList { if p.id != id { continue } phost, _, err := net.SplitHostPort(p.address) if err != nil { return false } candidateHost, _, err := net.SplitHostPort(peer) if err != nil { return false } if phost == candidateHost { return true } } return false }
func (s *store) filterAddr(addrs []string, filter string) ([]string, error) { host, port, err := net.SplitHostPort(filter) if err != nil { return nil, err } ip, err := net.ResolveIPAddr("ip", host) if err != nil { return nil, err } var joinPeers []string for _, addr := range addrs { joinHost, joinPort, err := net.SplitHostPort(addr) if err != nil { return nil, err } joinIp, err := net.ResolveIPAddr("ip", joinHost) if err != nil { return nil, err } // Don't allow joining ourselves if ip.String() == joinIp.String() && port == joinPort { continue } joinPeers = append(joinPeers, addr) } return joinPeers, nil }
// save a list of feeds to overwrite feeds.ini func SaveFeeds(feeds []FeedConfig) (err error) { conf := configparser.NewConfiguration() for _, feed := range feeds { if len(feed.Name) == 0 { // don't do feed with no name continue } sect := conf.NewSection("feed-" + feed.Name) if len(feed.proxy_type) > 0 { sect.Add("proxy-type", feed.proxy_type) } phost, pport, _ := net.SplitHostPort(feed.proxy_addr) sect.Add("proxy-host", phost) sect.Add("proxy-port", pport) host, port, _ := net.SplitHostPort(feed.Addr) sect.Add("host", host) sect.Add("port", port) sync := "0" if feed.sync { sync = "1" } sect.Add("sync", sync) interval := feed.sync_interval / time.Second sect.Add("sync-interval", fmt.Sprintf("%d", int(interval))) sect.Add("username", feed.username) sect.Add("password", feed.passwd) sect = conf.NewSection(feed.Name) for k, v := range feed.policy.rules { sect.Add(k, v) } } return configparser.Save(conf, "feeds.ini") }
func (p *Peer) Start() { peerHost, peerPort, _ := net.SplitHostPort(p.conn.LocalAddr().String()) servHost, servPort, _ := net.SplitHostPort(p.conn.RemoteAddr().String()) if p.inbound { p.host, p.port = packAddr(peerHost, peerPort) } else { p.host, p.port = packAddr(servHost, servPort) } err := p.pushHandshake() if err != nil { peerlogger.Debugln("Peer can't send outbound version ack", err) p.Stop() return } go p.HandleOutbound() // Run the inbound handler in a new goroutine go p.HandleInbound() // Run the general update handler go p.update() // Wait a few seconds for startup and then ask for an initial ping time.Sleep(2 * time.Second) p.writeMessage(ethwire.NewMessage(ethwire.MsgPingTy, "")) p.pingStartTime = time.Now() }
// Creates the url based on the url configuration. // Adds missing parts with defaults (scheme, host, port) func getUrl(defaultScheme string, defaultPath string, rawUrl string) (string, error) { urlStruct, err := url.Parse(rawUrl) if err != nil { return "", err } host := "" port := "" // If url doesn't have a scheme, host is written into path. For example: 192.168.3.7 if urlStruct.Host == "" { urlStruct.Host = urlStruct.Path urlStruct.Path = "" } // Checks if split host works _, _, err = net.SplitHostPort(urlStruct.Host) // Only does split host if no errors if err == nil { host, port, err = net.SplitHostPort(urlStruct.Host) if err != nil { return "", err } } else { host = urlStruct.Host } // Assign default host if not set if host == "" { host = "localhost" } // Assign default port if not set if port == "" { port = "9200" } // Assign default scheme if not set if urlStruct.Scheme == "" { urlStruct.Scheme = defaultScheme } // Assign default path if not set if urlStruct.Path == "" { urlStruct.Path = defaultPath } // Check if ipv6 if strings.Count(host, ":") > 1 && strings.Count(host, "]") == 0 { host = "[" + host + "]" } urlStruct.Host = host + ":" + port return urlStruct.String(), nil }
func (s *PeerSuite) TestResolveRecoverAddr(c *gc.C) { for _, testHostPort := range []string{"147.26.10.11:11370", "[fe80::d0dd:7dff:fefc:a828]:11370"} { reconAddr, err := net.ResolveTCPAddr("tcp", testHostPort) c.Assert(err, gc.IsNil) testHost, _, err := net.SplitHostPort(testHostPort) c.Assert(err, gc.IsNil) c.Assert(reconAddr.Port, gc.Equals, 11370) r := &Recover{ RemoteAddr: reconAddr, RemoteConfig: &Config{ HTTPPort: 8080, }, } hkpHostPort, err := r.HkpAddr() c.Assert(err, gc.IsNil) hkpAddr, err := net.ResolveTCPAddr("tcp", hkpHostPort) c.Assert(err, gc.IsNil) hkpHost, _, err := net.SplitHostPort(hkpHostPort) c.Assert(err, gc.IsNil) c.Assert(hkpAddr.Port, gc.Equals, 8080) c.Assert(reconAddr.IP, gc.DeepEquals, hkpAddr.IP) c.Assert(testHost, gc.Equals, hkpHost) } }
func (p *Peer) Start() { peerHost, peerPort, _ := net.SplitHostPort(p.conn.LocalAddr().String()) servHost, servPort, _ := net.SplitHostPort(p.conn.RemoteAddr().String()) if p.inbound { p.host, p.port = packAddr(peerHost, peerPort) } else { p.host, p.port = packAddr(servHost, servPort) } err := p.pushHandshake() if err != nil { ethutil.Config.Log.Debugln("Peer can't send outbound version ack", err) p.Stop() return } // Run the outbound handler in a new goroutine go p.HandleOutbound() // Run the inbound handler in a new goroutine go p.HandleInbound() }
// alertmanagersFromGroup extracts a list of alertmanagers from a target group and an associcated // AlertmanagerConfig. func alertmanagerFromGroup(tg *config.TargetGroup, cfg *config.AlertmanagerConfig) ([]alertmanager, error) { var res []alertmanager for _, lset := range tg.Targets { // Set configured scheme as the initial scheme label for overwrite. lset[model.SchemeLabel] = model.LabelValue(cfg.Scheme) lset[pathLabel] = model.LabelValue(postPath(cfg.PathPrefix)) // Combine target labels with target group labels. for ln, lv := range tg.Labels { if _, ok := lset[ln]; !ok { lset[ln] = lv } } lset := relabel.Process(lset, cfg.RelabelConfigs...) if lset == nil { continue } // addPort checks whether we should add a default port to the address. // If the address is not valid, we don't append a port either. addPort := func(s string) bool { // If we can split, a port exists and we don't have to add one. if _, _, err := net.SplitHostPort(s); err == nil { return false } // If adding a port makes it valid, the previous error // was not due to an invalid address and we can append a port. _, _, err := net.SplitHostPort(s + ":1234") return err == nil } // If it's an address with no trailing port, infer it based on the used scheme. if addr := string(lset[model.AddressLabel]); addPort(addr) { // Addresses reaching this point are already wrapped in [] if necessary. switch lset[model.SchemeLabel] { case "http", "": addr = addr + ":80" case "https": addr = addr + ":443" default: return nil, fmt.Errorf("invalid scheme: %q", cfg.Scheme) } lset[model.AddressLabel] = model.LabelValue(addr) } if err := config.CheckTargetAddress(lset[model.AddressLabel]); err != nil { return nil, err } // Meta labels are deleted after relabelling. Other internal labels propagate to // the target which decides whether they will be part of their label set. for ln := range lset { if strings.HasPrefix(string(ln), model.MetaLabelPrefix) { delete(lset, ln) } } res = append(res, alertmanagerLabels(lset)) } return res, nil }
func (d *Dovecot) gatherServer(addr string, acc telegraf.Accumulator, qtype string, filter string) error { _, _, err := net.SplitHostPort(addr) if err != nil { return fmt.Errorf("Error: %s on url %s\n", err, addr) } c, err := net.DialTimeout("tcp", addr, defaultTimeout) if err != nil { return fmt.Errorf("Unable to connect to dovecot server '%s': %s", addr, err) } defer c.Close() // Extend connection c.SetDeadline(time.Now().Add(defaultTimeout)) msg := fmt.Sprintf("EXPORT\t%s", qtype) if len(filter) > 0 { msg += fmt.Sprintf("\t%s=%s", qtype, filter) } msg += "\n" c.Write([]byte(msg)) var buf bytes.Buffer io.Copy(&buf, c) host, _, _ := net.SplitHostPort(addr) return gatherStats(&buf, acc, host, qtype) }
func main() { flag.Parse() IPs = parseIPs() MinPort, MaxPort = parsePorts() thisAddr = canonicalAddr(*flag_addr, IPs) var err error thisHost, _, err = net.SplitHostPort(thisAddr) util.FatalErr(err) DetectMumax() DetectGPUs() LoadJobs() http.HandleFunc("/do/", HandleRPC) http.HandleFunc("/", HandleStatus) httpfs.RegisterHandlers() // Listen and serve on all interfaces go func() { log.Println("serving at", thisAddr) // try to listen and serve on all interfaces other than thisAddr // this is for convenience, errors are not fatal. _, p, err := net.SplitHostPort(thisAddr) Fatal(err) ips := util.InterfaceAddrs() for _, ip := range ips { addr := net.JoinHostPort(ip, p) if addr != thisAddr { // skip thisAddr, will start later and is fatal on error go func() { err := http.ListenAndServe(addr, nil) if err != nil { log.Println("info:", err, "(but still serving other interfaces)") } }() } } // only on thisAddr, this server's unique address, // we HAVE to be listening. Fatal(http.ListenAndServe(thisAddr, nil)) }() ProbePeer(thisAddr) // make sure we have ourself as peer go FindPeers(IPs, MinPort, MaxPort) go RunComputeService() go LoopWatchdog() go RunShareDecay() // re-load jobs every hour so we don't stall on very exceptional circumstances go func() { for { time.Sleep(1 * time.Hour) LoadJobs() } }() <-make(chan struct{}) // wait forever }
// sanitizeBindAddr cleans up the BindAddr parameter and appends a port // if necessary based on the advertised port. func sanitizeBindAddr(bindAddr string, addr string) (string, error) { aurl, err := url.Parse(addr) if err != nil { return "", err } // If it is a valid host:port simply return with no further checks. bhost, bport, err := net.SplitHostPort(bindAddr) if err == nil && bhost != "" { return bindAddr, nil } // SplitHostPort makes the host optional, but we don't want that. if bhost == "" && bport != "" { return "", fmt.Errorf("IP required can't use a port only") } // bindAddr doesn't have a port if we reach here so take the port from the // advertised URL. _, aport, err := net.SplitHostPort(aurl.Host) if err != nil { return "", err } return net.JoinHostPort(bindAddr, aport), nil }
func (pconn *persistentConn) listenActive() (*net.TCPListener, error) { listenAddr := pconn.config.ActiveListenAddr localAddr := pconn.controlConn.LocalAddr().String() localHost, localPort, err := net.SplitHostPort(localAddr) if err != nil { return nil, ftpError{err: fmt.Errorf("error splitting local address: %s (%s)", err, localAddr)} } if listenAddr == ":" { listenAddr = localAddr } else if listenAddr[len(listenAddr)-1] == ':' { listenAddr = net.JoinHostPort(listenAddr[0:len(listenAddr)-1], localPort) } else if listenAddr[0] == ':' { listenAddr = net.JoinHostPort(localHost, listenAddr[1:]) } tcpAddr, err := net.ResolveTCPAddr("tcp", listenAddr) if err != nil { return nil, ftpError{err: fmt.Errorf("error parsing active listen addr: %s (%s)", err, listenAddr)} } listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { return nil, ftpError{err: fmt.Errorf("error listening on %s for active transfer: %s", listenAddr, err)} } pconn.debug("listening on %s for active connection", listener.Addr().String()) listenHost, listenPortStr, err := net.SplitHostPort(listener.Addr().String()) if err != nil { return nil, ftpError{err: fmt.Errorf("error splitting listener addr: %s (%s)", err, listener.Addr().String())} } listenPort, err := strconv.Atoi(listenPortStr) if err != nil { return nil, ftpError{err: fmt.Errorf("error parsing listen port: %s (%s)", err, listenPortStr)} } hostIP := net.ParseIP(listenHost) if hostIP == nil { return nil, ftpError{err: fmt.Errorf("failed parsing host IP %s", listenHost)} } hostIPv4 := hostIP.To4() if hostIPv4 == nil { if err := pconn.sendCommandExpected(200, "EPRT |%d|%s|%d|", 2, listenHost, listenPort); err != nil { return nil, err } } else { err := pconn.sendCommandExpected(200, "PORT %d,%d,%d,%d,%d,%d", hostIPv4[0], hostIPv4[1], hostIPv4[2], hostIPv4[3], listenPort>>8, listenPort&0xFF, ) if err != nil { return nil, err } } return listener, nil }
func officialAddr(unresolvedAddr string, resolvedAddr net.Addr) (*util.UnresolvedAddr, error) { unresolvedHost, unresolvedPort, err := net.SplitHostPort(unresolvedAddr) if err != nil { return nil, err } resolvedHost, resolvedPort, err := net.SplitHostPort(resolvedAddr.String()) if err != nil { return nil, err } var host string if unresolvedHost != "" { // A host was provided, use it. host = unresolvedHost } else { // A host was not provided. Ask the system, and fall back to the listener. if hostname, err := os.Hostname(); err == nil { host = hostname } else { host = resolvedHost } } var port string if unresolvedPort != "0" { // A port was provided, use it. port = unresolvedPort } else { // A port was not provided, but the system assigned one. port = resolvedPort } return util.NewUnresolvedAddr(resolvedAddr.Network(), net.JoinHostPort(host, port)), nil }
func processConnection(c net.Conn) { ip, _, err := net.SplitHostPort(c.RemoteAddr().String()) if err != nil { log.Println(err) return } peon, err := findConnection(c) if err != nil { log.Println("Connection errored out") return } if peon != nil { remote, _, _ := net.SplitHostPort(peon.RemoteHost.RemoteAddr().String()) log.Println("Found a host already connected with that address", remote) c.Write([]byte("Already Accepted a connection from this host\n")) defer c.Close() return } log.Println("Processing Connection from: ", ip) /*hostChannel := make (chan string) host := containers.Host{c, nil, "test", 10000, hostChannel, [32]byte , new(sync.RWMutex)} modPool.Lock() hostPool = append(hostPool, host) modPool.Unlock() go readConnection(c, host) log.Println("Finished Processing Connection")*/ }
// standardAddress turns the accepted host and port patterns // into a format accepted by net.Dial. func standardAddress(str string) (host, port string, err error) { var schemePort, splitPort string if strings.HasPrefix(str, "https://") { schemePort = "https" str = str[8:] } else if strings.HasPrefix(str, "http://") { schemePort = "http" str = str[7:] } host, splitPort, err = net.SplitHostPort(str) if err != nil { host, splitPort, err = net.SplitHostPort(str + ":") // tack on empty port } if err != nil { // ¯\_(ツ)_/¯ host = str } if splitPort != "" { port = splitPort } else { port = schemePort } return }
func (s *DockerServer) initSwarmNode(listenAddr, advertiseAddr string) (swarm.Node, error) { if listenAddr == "" { listenAddr = "127.0.0.1:0" } var err error s.swarmServer, err = newSwarmServer(s, listenAddr) if err != nil { return swarm.Node{}, err } if advertiseAddr == "" { advertiseAddr = s.SwarmAddress() } hostPart, portPart, err := net.SplitHostPort(advertiseAddr) if err != nil { hostPart = advertiseAddr } if portPart == "" || portPart == "0" { _, portPart, _ = net.SplitHostPort(s.SwarmAddress()) } s.nodeID = s.generateID() return swarm.Node{ ID: s.nodeID, Status: swarm.NodeStatus{ State: swarm.NodeStateReady, }, ManagerStatus: &swarm.ManagerStatus{ Addr: fmt.Sprintf("%s:%s", hostPart, portPart), }, }, nil }
// NewReplacer makes a new replacer based on r and rr. // Do not create a new replacer until r and rr have all // the needed values, because this function copies those // values into the replacer. rr may be nil if it is not // available. emptyValue should be the string that is used // in place of empty string (can still be empty string). func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Replacer { rep := replacer{ replacements: map[string]string{ "{method}": r.Method, "{scheme}": func() string { if r.TLS != nil { return "https" } return "http" }(), "{host}": r.Host, "{path}": r.URL.Path, "{query}": r.URL.RawQuery, "{fragment}": r.URL.Fragment, "{proto}": r.Proto, "{remote}": func() string { if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" { return fwdFor } host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { return r.RemoteAddr } return host }(), "{port}": func() string { _, port, err := net.SplitHostPort(r.RemoteAddr) if err != nil { return "" } return port }(), "{uri}": r.URL.RequestURI(), "{when}": func() string { return time.Now().Format(timeFormat) }(), "{file}": func() string { _, file := path.Split(r.URL.Path) return file }(), "{dir}": func() string { dir, _ := path.Split(r.URL.Path) return dir }(), }, emptyValue: emptyValue, } if rr != nil { rep.replacements["{status}"] = strconv.Itoa(rr.status) rep.replacements["{size}"] = strconv.Itoa(rr.size) rep.replacements["{latency}"] = time.Since(rr.start).String() } // Header placeholders for header, val := range r.Header { rep.replacements[headerReplacer+header+"}"] = strings.Join(val, ",") } return rep }
func init() { // TCP setup. tcp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(r.URL.Path[1:])) })) u, err := url.Parse(tcp.URL) if err != nil { panic(fmt.Sprintf("failed to parse: %v", err)) } _, tcpServerPort, err = net.SplitHostPort(u.Host) if err != nil { panic(fmt.Sprintf("failed to parse: %v", err)) } // UDP setup. udp, err := newUDPEchoServer() if err != nil { panic(fmt.Sprintf("failed to make a UDP server: %v", err)) } _, udpServerPort, err = net.SplitHostPort(udp.LocalAddr().String()) if err != nil { panic(fmt.Sprintf("failed to parse: %v", err)) } go udp.Loop() }
func TestNewReverseProxy(t *testing.T) { backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) hostname, _, _ := net.SplitHostPort(r.Host) w.Write([]byte(hostname)) })) defer backend.Close() backendURL, _ := url.Parse(backend.URL) backendHostname, backendPort, _ := net.SplitHostPort(backendURL.Host) backendHost := net.JoinHostPort(backendHostname, backendPort) proxyURL, _ := url.Parse(backendURL.Scheme + "://" + backendHost + "/") proxyHandler := NewReverseProxy(proxyURL) setProxyUpstreamHostHeader(proxyHandler, proxyURL) frontend := httptest.NewServer(proxyHandler) defer frontend.Close() getReq, _ := http.NewRequest("GET", frontend.URL, nil) res, _ := http.DefaultClient.Do(getReq) bodyBytes, _ := ioutil.ReadAll(res.Body) if g, e := string(bodyBytes), backendHostname; g != e { t.Errorf("got body %q; expected %q", g, e) } }
func (f *ratelimiter) verifyAllowed(resp http.ResponseWriter, req *http.Request, next filters.Next) error { host, _, err := net.SplitHostPort(req.Host) if err != nil { host = req.Host } client, _, _ := net.SplitHostPort(req.RemoteAddr) now := time.Now() f.mx.Lock() defer f.mx.Unlock() period := f.hostPeriods[host] if period == 0 { return fmt.Errorf("Access to %v not allowed", host) } var hostAccesses map[string]time.Time _hostAccesses, found := f.hostAccessesByClient.Get(client) if found { hostAccesses = _hostAccesses.(map[string]time.Time) } else { hostAccesses = make(map[string]time.Time) } allowed := now.Sub(hostAccesses[host]) > period if allowed { hostAccesses[host] = now f.hostAccessesByClient.Add(client, hostAccesses) } if !allowed { return fmt.Errorf("Rate limit for %v exceeded", host) } return nil }
// standardAddress parses an address string into a structured format with separate // host, and port portions, as well as the original input string. func standardAddress(str string) (address, error) { var err error // first check for scheme and strip it off input := str // separate host and port host, port, err := net.SplitHostPort(str) if err != nil { host, port, err = net.SplitHostPort(str + ":") // no error check here; return err at end of function } if len(host) > 255 { return address{}, fmt.Errorf("specified address is too long: %d > 255", len(host)) } _, d := dns.IsDomainName(host) if !d { return address{}, fmt.Errorf("host is not a valid domain: %s", host) } // see if we can set port based off scheme if port == "" { port = "53" } return address{Original: input, Host: strings.ToLower(dns.Fqdn(host)), Port: port}, err }
func (c *ProxyConn) writeProxyProtocolHeader() error { s := c.Conn.LocalAddr() saddr, sport, err := net.SplitHostPort(s.String()) if err != nil { return err } d := c.Conn.RemoteAddr() daddr, dport, err := net.SplitHostPort(d.String()) if err != nil { return err } raddr, ok := d.(*net.TCPAddr) if !ok { return errors.New("proxyconn: must be tcp or tcp6") } var tcpStr string if rip4 := raddr.IP.To4(); len(rip4) == net.IPv4len { tcpStr = "TCP4" } else if len(raddr.IP) == net.IPv6len { tcpStr = "TCP6" } else { return errors.New("proxyconn: unknown length") } _, err = fmt.Fprintf(c.Conn, "PROXY %s %s %s %s %s\r\n", tcpStr, saddr, daddr, sport, dport) return err }