Example #1
0
// We accept 'ssl'/'tls' as a protocol; it implies 'tcp' as the underlying
// protocol.
func dial(proto, addr, laddr string, tmout time.Duration) (net.Conn, error) {
	// Set up our dialer options; we may need a local address and/or
	// a connection timeout.
	// TODO: happy eyeballs support, ie dialer.DualStack? This might be
	// worth a command line switch.
	var dialer net.Dialer
	dialer.Timeout = tmout
	if laddr != "" {
		a, e := ResolveAddr(proto, laddr)
		if e != nil {
			return nil, e
		}
		dialer.LocalAddr = a
	}

	switch proto {
	case "ssl", "tls":
		// For testing I do not want to have to verify anything
		// about the target certificates. I have other tools for
		// that.
		cfg := tls.Config{InsecureSkipVerify: true}
		return tls.DialWithDialer(&dialer, "tcp", addr, &cfg)
	case "sslver", "tlsver":
		return tls.DialWithDialer(&dialer, "tcp", addr, nil)
	}
	return dialer.Dial(proto, addr)
}
Example #2
0
// call sends an RPC to the rpcname handler on server srv
// with arguments args, waits for the reply, and leaves the
// reply in reply. the reply argument should be a pointer
// to a reply structure.
//
// the return value is true if the server responded, and false
// if call() was not able to contact the server. in particular,
// the replys contents are only valid if call() returned true.
func call(srv string, name string, clientAddr *string,
	args interface{}, reply interface{}) bool {
	var d net.Dialer
	if clientAddr != nil {
		// need to specify the port at 0
		// let the kernel choose a random port
		addr, _ := net.ResolveTCPAddr("tcp", ip(*clientAddr)+":0")
		d.LocalAddr = addr
		// also set the timeout
		d.Timeout = timeOut
	}

	// tcp dial

	conn, err := d.Dial("tcp", srv)
	if err != nil {
		err1 := err.(*net.OpError)
		if err1.Err != syscall.ENOENT && err1.Err != syscall.ECONNREFUSED {
			fmt.Printf("paxos Dial(%s) failed: %v\n", srv, err1)
		}
		return false
	}
	client := rpc.NewClient(conn)
	defer conn.Close()
	defer client.Close()

	// rpc call

	rpcErrChan := make(chan error)
	go func() {
		rpcErrChan <- client.Call(name, args, reply)
	}()

	select {
	case <-time.After(timeOut):
		return false

	case err := <-rpcErrChan:
		if err == nil {
			return true
		}
		fmt.Println(err)
		return false
	}
}
Example #3
0
func connectIPCServer(config *ProcessorConfig) error {
	os.MkdirAll(config.Home+"/ipc", 0770)
	remote := config.Home + "/ipc/" + config.ClusterName + ".sock"
	local := config.Home + "/ipc/" + config.Name + ".sock"
	//Unix client would connect failed if the unix socket file exist, so remove it first
	os.Remove(local)

	var dialer net.Dialer
	//Bind local unix socket
	dialer.LocalAddr = &net.UnixAddr{local, "unix"}
	c, err := dialer.Dial("unix", remote)
	if nil != err {
		glog.Warningf("Failed to connect ssf framework for reason:%v", err)
		return err
	}
	glog.Infof("SSF framwork IPC server connected.")
	procipc.unixConn = c
	return nil
}
Example #4
0
func Client(cfg *Config) *Conn {
	if cfg == nil {
		cfg = NewConfig("__idiot__")
	}
	if cfg.Me == nil || cfg.Me.Nick == "" || cfg.Me.Ident == "" {
		cfg.Me = state.NewNick("__idiot__")
		cfg.Me.Ident = "goirc"
		cfg.Me.Name = "Powered by GoIRC"
	}

	dialer := new(net.Dialer)
	if cfg.LocalAddr != "" {
		if !hasPort(cfg.LocalAddr) {
			cfg.LocalAddr += ":0"
		}

		local, err := net.ResolveTCPAddr("tcp", cfg.LocalAddr)
		if err == nil {
			dialer.LocalAddr = local
		} else {
			logging.Error("irc.Client(): Cannot resolve local address %s: %s", cfg.LocalAddr, err)
		}
	}

	conn := &Conn{
		cfg:         cfg,
		dialer:      dialer,
		in:          make(chan *Line, 32),
		out:         make(chan string, 32),
		intHandlers: handlerSet(),
		fgHandlers:  handlerSet(),
		bgHandlers:  handlerSet(),
		stRemovers:  make([]Remover, 0, len(stHandlers)),
		lastsent:    time.Now(),
	}
	conn.addIntHandlers()
	conn.initialise()
	return conn
}
Example #5
0
//发送请求
func (h *Httpx) Send() (response *http.Response, err error) {
	if h.Url == "" {
		return nil, errors.New("URL is empty")
	}

	defer func() {
		if err != nil && h.ClientIP != "" {
			err = errors.New(err.Error() + " client ip is " + h.ClientIP)
		}
	}()

	var req *http.Request

	if h.Method == "POST" {
		req, _ = http.NewRequest("POST", h.Url, strings.NewReader(h.PostData.Encode()))
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	} else {
		req, _ = http.NewRequest(h.Method, h.Url, nil)
	}

	//headers
	if len(h.Headers) > 0 {
		for k, v := range h.Headers {
			req.Header.Set(k, v)
		}
	}

	//cookies
	if len(h.Cookies) > 0 {
		for _, v := range h.Cookies {
			req.AddCookie(v)
		}
	}

	transport := &http.Transport{}

	//是否使用代理
	if h.ProxyUrl != "" {
		proxy, err := url.Parse(h.ProxyUrl)
		if err != nil {
			return nil, err
		}
		transport.Proxy = http.ProxyURL(proxy)
	}

	//设置超时时间
	dialer := net.Dialer{
		Timeout:  time.Duration(h.Timeout) * time.Second,
		Deadline: time.Now().Add(time.Duration(h.Timeout) * time.Second),
	}
	//是否使用指定的IP发送请求
	if h.ClientIP != "" {
		transport.Dial = func(network, address string) (net.Conn, error) {
			//本地地址  本地外网IP
			lAddr, err := net.ResolveTCPAddr(network, h.ClientIP+":0")
			if err != nil {
				return nil, err
			}
			dialer.LocalAddr = lAddr
			return dialer.Dial(network, address)
		}
	} else {
		transport.Dial = func(network, address string) (net.Conn, error) {
			return dialer.Dial(network, address)
		}
	}

	client := &http.Client{
		Transport: transport,
	}
	response, err = client.Do(req)
	return response, err

}