本文整理汇总了Golang中net/textproto.NewConn函数的典型用法代码示例。如果您正苦于以下问题:Golang NewConn函数的具体用法?Golang NewConn怎么用?Golang NewConn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewConn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getTestClient
// Returns a test client and the end of a connection pipe
func getTestClient() (*transaction, *textproto.Conn) {
sc, cc := net.Pipe()
sconn, cconn := textproto.NewConn(sc), textproto.NewConn(cc)
client := &transaction{
Mode: stateHELO,
Message: new(mailbox.Message),
text: sconn,
host: &mockHost{
QueryMock: func(addr *mail.Address) int {
switch strings.Split(addr.Address, "@")[0] {
case "not_found":
return mailbox.QueryNotFound
case "not_local":
return mailbox.QueryNotLocal
case "success":
return mailbox.QuerySuccess
}
return mailbox.QueryError
},
SettingsMock: func() jamon.Group { return jamon.Group{} },
DigestMock: func(c *transaction) error { return nil },
},
}
return client, cconn
}
示例2: DialTimeout
// DialTimeout initializes the connection to the specified ftp server address.
//
// It is generally followed by a call to Login() as most FTP commands require
// an authenticated user.
func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
tconn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}
conn := textproto.NewConn(tconn)
a := strings.SplitN(addr, ":", 2)
c := &ServerConn{
conn: conn,
host: a[0],
timeout: timeout,
features: make(map[string]string),
}
_, _, err = c.conn.ReadResponse(StatusReady)
if err != nil {
c.Quit()
return nil, err
}
err = c.feat()
if err != nil {
c.Quit()
return nil, err
}
return c, nil
}
示例3: SendStartTLS
func SendStartTLS(conn net.Conn, config *tls.Config) (econn *textproto.Conn, state tls.ConnectionState, err error) {
_, err = io.WriteString(conn, "STARTTLS\r\n")
if err == nil {
r := bufio.NewReader(conn)
var line string
line, err = r.ReadString(10)
if strings.HasPrefix(line, "382 ") {
// we gud
tconn := tls.Client(conn, config)
err = tconn.Handshake()
if err == nil {
// tls okay
state = tconn.ConnectionState()
econn = textproto.NewConn(tconn)
} else {
log.Println("STARTTLS failed", err)
tconn.Close()
}
} else {
// it won't do tls
err = TlsNotSupported
}
r = nil
}
return
}
示例4: syncPull
// do a oneshot pull based sync with another server
func (self NNTPDaemon) syncPull(proxy_type, proxy_addr, remote_addr string) {
c, err := self.dialOut(proxy_type, proxy_addr, remote_addr)
if err == nil {
conn := textproto.NewConn(c)
// we connected
nntp := createNNTPConnection()
nntp.name = remote_addr + "-sync"
// do handshake
_, reader, err := nntp.outboundHandshake(conn)
if reader {
// we can do it
err = nntp.scrapeServer(self, conn)
if err == nil {
log.Println(nntp.name, "Scrape successful")
nntp.Quit(conn)
} else {
log.Println(nntp.name, "scrape failed", err)
conn.Close()
}
} else if err == nil {
// we can't do it
log.Println(nntp.name, "does not support reader mode, cancel scrape")
nntp.Quit(conn)
} else {
// error happened
log.Println(nntp.name, "error occurred when scraping", err)
}
}
}
示例5: TestClientNotify
// It should reply using the attached network connection
func TestClientNotify(t *testing.T) {
sc, cc := net.Pipe()
cconn := textproto.NewConn(cc)
testClient := &transaction{text: textproto.NewConn(sc)}
go testClient.notify(reply{200, "Hello"})
msg, err := cconn.ReadLine()
if err != nil {
t.Errorf("Error reading end of pipe (%s)", err)
}
if msg != "200 Hello" {
t.Errorf("Got '%s' but expected '%s'", msg, "200 Hello")
}
}
示例6: persistFeed
func (self NNTPDaemon) persistFeed(conf FeedConfig, mode string) {
for {
if self.running {
conn, err := self.dialOut(conf.proxy_type, conf.proxy_addr, conf.addr)
if err != nil {
time.Sleep(time.Second * 5)
continue
}
nntp := createNNTPConnection()
nntp.policy = conf.policy
nntp.name = conf.name + "-" + mode
c := textproto.NewConn(conn)
stream, reader, err := nntp.outboundHandshake(c)
if err == nil {
if mode == "reader" && !reader {
log.Println(nntp.name, "we don't support reader on this feed, dropping")
return
}
self.register_outfeed <- nntp
nntp.runConnection(self, false, stream, reader, mode, c)
self.deregister_outfeed <- nntp
} else {
log.Println("error doing outbound hanshake", err)
}
}
time.Sleep(1 * time.Second)
}
}
示例7: connect
//Connect establishes a connection with the local Freeswitch server.
func (client *Client) connect() (err error) {
//Connect to Freeswitch Event Socket.
conn, err := net.DialTimeout("tcp", client.addr, time.Duration(5*time.Second))
if err != nil {
return
}
//Convert the raw TCP connection to a textproto connection.
client.connMu.Lock()
defer client.connMu.Unlock()
if client.eventConn != nil {
client.eventConn.Close()
}
client.eventConn = textproto.NewConn(conn)
//Read the welcome message.
resp, err := client.eventConn.ReadMIMEHeader()
if err != nil {
return
}
//Send authentication request to server.
client.eventConn.PrintfLine("auth %s\r\n", client.password)
if resp, err = client.eventConn.ReadMIMEHeader(); err != nil {
return
}
//Check the command was processed OK.
if resp.Get("Reply-Text") == "+OK accepted" {
return
}
return errors.New("Authentication failed: " + resp.Get("Reply-Text"))
}
示例8: NewAmiConn
func NewAmiConn(ctx context.Context, conn io.ReadWriteCloser, c *AmiServer) *AmiConn {
var (
sctx context.Context
cancel context.CancelFunc
)
sctx, cancel = context.WithCancel(ctx)
amic := &AmiConn{
Context: sctx,
cancel: cancel,
uuid: uuid.New("C"),
srv: c,
messageCh: make(chan textproto.MIMEHeader, 100),
connRaw: conn,
conn: textproto.NewConn(conn),
}
go amic.doHeartBeat()
go amic.doReader()
go amic.doWriter()
go func(amic *AmiConn) {
<-amic.Done()
amic.Close()
}(amic)
amic.conn.PrintfLine("Asterisk Call Manager")
return amic
}
示例9: TestServerCreateClient
// Should create a client that picks up commands
// and greet the connection with 220 status
func TestServerCreateClient(t *testing.T) {
var wg sync.WaitGroup
cc, sc := mocks.Pipe(
&mocks.Conn{RAddr: "1.1.1.1:123"},
&mocks.Conn{RAddr: "1.1.1.1:123"},
)
cconn := textproto.NewConn(cc)
testServer := server{
spec: commandSpec{
"EXIT": func(ctx *transaction, params string) error {
return io.EOF
},
},
config: jamon.Group{"host": "mecca.local"},
}
wg.Add(1)
go func() {
testServer.createTransaction(sc)
wg.Done()
}()
_, _, err := cconn.ReadResponse(220)
if err != nil {
t.Errorf("Expected code 220 but got %+v", err)
}
cconn.PrintfLine("EXIT")
wg.Wait()
}
示例10: newOutboundConn
// create a new connection from an established connection
func newOutboundConn(c net.Conn, s *Server, conf *config.FeedConfig) Conn {
sname := s.Name()
if len(sname) == 0 {
sname = "nntp.anon.tld"
}
storage := s.Storage
if storage == nil {
storage = store.NewNullStorage()
}
return &v1OBConn{
conf: conf,
C: v1Conn{
hooks: s,
state: ConnState{
FeedName: conf.Name,
HostName: conf.Addr,
Open: true,
},
serverName: sname,
storage: storage,
C: textproto.NewConn(c),
conn: c,
hdrio: message.NewHeaderIO(),
},
}
}
示例11: serve
func (s *Server) serve(conn net.Conn) {
c := textproto.NewConn(conn)
// state controlled processing here
for {
cr, err := NewCommandRequest(c)
if err != nil {
log.Println(err)
continue
}
// start of a muxer
// s.handler[verb](c)
sr := &StatusResponse{}
if s.mux == nil {
panic("awp: no servemux specified")
}
s.mux.ServeAWP(cr, sr)
c.PrintfLine("%d %s\r\n", sr.Code, sr.Status)
}
}
示例12: NewTestConnection
// NewTestConnection is for test facilitation.
// Creates a server and then dials the server, returning the connection,
// allowing test to inject state and wait for an expected response
// The connection must be started manually with `go conn.Start()`
// once desired state has been injected
func NewTestConnection(transcript io.Writer) (s *Server, clientConn *textproto.Conn, serverConn *conn.Conn, server *Server, err error) {
mStore := mailstore.NewDummyMailstore()
s = NewServer(mStore)
s.Addr = ":10143"
s.Transcript = transcript
if err = s.Listen(); err != nil {
return nil, nil, nil, nil, err
}
c, err := net.Dial("tcp4", "localhost:10143")
if err != nil {
return nil, nil, nil, nil, err
}
textc := textproto.NewConn(c)
clientConn = textc
conn, err := s.listener.Accept()
if err != nil {
return nil, nil, nil, nil, err
}
fmt.Fprintf(s.Transcript, "Client connected\n")
serverConn, err = s.newConn(conn)
return s, clientConn, serverConn, s, nil
}
示例13: upgradeTLS
// handle inbound STARTTLS command
func upgradeTLS(c *v1Conn, line string, hooks EventHooks) (err error) {
if c.tlsConfig == nil {
err = c.printfLine("%s TLS not supported", RPL_TLSRejected)
} else {
err = c.printfLine("%s Continue with TLS Negotiation", RPL_TLSContinue)
if err == nil {
tconn := tls.Server(c.conn, c.tlsConfig)
err = tconn.Handshake()
if err == nil {
// successful tls handshake
c.tlsConn = tconn
c.C = textproto.NewConn(c.tlsConn)
} else {
// tls failed
log.WithFields(log.Fields{
"pkg": "nntp-conn",
"addr": c.conn.RemoteAddr(),
"state": c.state,
}).Warn("TLS Handshake failed ", err)
// fall back to plaintext
err = nil
}
}
}
return
}
示例14: NewLilyConn
func NewLilyConn(address string) *LilyConn {
localAddress, _ := net.ResolveTCPAddr("0.0.0.0:0")
lilyAddress, err := net.ResolveTCPAddr(address)
if err != nil {
logger.Logf("failed resolving %s: %s", address, err)
return nil
}
tcpConn, err := net.DialTCP("tcp", localAddress, lilyAddress)
if err != nil {
logger.Logf("failed connecting to %s: %s", address, err)
return nil
}
var newLilyConn LilyConn
newLilyConn.tcpConn = tcpConn
newLilyConn.textConn = textproto.NewConn(tcpConn)
newLilyConn.incomingChannel = make(chan *LilyMessage, 100)
newLilyConn.outgoingChannel = make(chan *LilyMessage, 100)
newLilyConn.handleMap = make(map[string]*LilyHandle)
newLilyConn.SendOptions()
go newLilyConn.Dispatch()
return &newLilyConn
}
示例15: newInboundConn
func newInboundConn(s *Server, c net.Conn) Conn {
sname := s.Name
if len(sname) == 0 {
sname = "nntp.anon.tld"
}
storage := s.Storage
if storage == nil {
storage = store.NewNullStorage()
}
return &v1IBConn{
C: v1Conn{
authenticated: true,
serverName: sname,
storage: storage,
acceptor: s.Acceptor,
hdrio: message.NewHeaderIO(),
C: textproto.NewConn(c),
conn: c,
cmds: map[string]lineHandlerFunc{
"IHAVE": nntpRecvArticle,
"POST": nntpPostArticle,
"MODE": switchModeInbound,
"QUIT": quitConnection,
"CAPABILITIES": sendCapabilities,
"CHECK": streamingLine,
"TAKETHIS": streamingLine,
"LIST": newsgroupList,
"GROUP": switchNewsgroup,
},
},
}
}