本文整理汇总了Golang中net/textproto.Conn.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.Close方法的具体用法?Golang Conn.Close怎么用?Golang Conn.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/textproto.Conn
的用法示例。
在下文中一共展示了Conn.Close方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: startReader
func (self *nntpConnection) startReader(daemon *NNTPDaemon, conn *textproto.Conn) {
log.Println(self.name, "run reader mode")
for {
var err error
select {
case chnl := <-self.die:
// we were asked to die
// send quit
conn.PrintfLine("QUIT")
chnl <- true
break
case msgid := <-self.article:
// next article to ask for
log.Println(self.name, "obtaining", msgid)
self.messageSetPendingState(msgid, "article")
err = self.requestArticle(daemon, conn, msgid)
self.messageSetProcessed(msgid)
if err != nil {
log.Println(self.name, "error while in reader mode:", err)
break
}
}
}
// close connection
conn.Close()
}
示例2: handle
func handle(c *textproto.Conn, verifyOnly bool) {
defer c.Close()
defer func() {
if r := recover(); r != nil {
log.Println("Something went wrong:", r)
}
}()
converse(c, verifyOnly)
}
示例3: startReader
func (self *nntpConnection) startReader(daemon NNTPDaemon, conn *textproto.Conn) {
log.Println(self.name, "run reader mode")
var err error
for err == nil {
// next article to ask for
msgid := <-self.article
err = self.requestArticle(daemon, conn, msgid)
}
// report error and close connection
log.Println(self.name, "error while in reader mode:", err)
conn.Close()
}
示例4: CloseConn
func CloseConn(conn *textproto.Conn) error {
id, err := conn.Cmd("close")
if err != nil {
return err
}
conn.StartResponse(id)
conn.EndResponse(id)
err = conn.Close()
if err != nil {
return err
}
return nil
}
示例5: handleStreaming
// handle streaming events
// this function should send only
func (self *nntpConnection) handleStreaming(daemon *NNTPDaemon, conn *textproto.Conn) (err error) {
for err == nil {
select {
case chnl := <-self.die:
// someone asked us to die
conn.PrintfLine("QUIT")
conn.Close()
chnl <- true
return
case msgid := <-self.check:
err = self.handleStreamEvent(nntpCHECK(msgid), daemon, conn)
self.messageSetPendingState(msgid, "check")
case msgid := <-self.takethis:
self.messageSetPendingState(msgid, "takethis")
err = self.handleStreamEvent(nntpTAKETHIS(msgid), daemon, conn)
}
}
return
}
示例6: handleConnection
func (s *server) handleConnection(p *textproto.Conn) {
id := p.Next()
p.StartRequest(id)
p.EndRequest(id)
p.StartResponse(id)
p.PrintfLine("OK MPD gompd0.1")
p.EndResponse(id)
endIdle := make(chan bool)
inIdle := false
defer p.Close()
for {
id := p.Next()
p.StartRequest(id)
req, err := s.readRequest(p)
if err != nil {
return
}
// We need to do this inside request because idle response
// may not have ended yet, but it will end after the following.
if inIdle {
endIdle <- true
}
p.EndRequest(id)
if req.typ == idle {
inIdle = true
go s.writeIdleResponse(p, id, endIdle, req.args[1:])
// writeIdleResponse does it's own StartResponse/EndResponse
continue
}
p.StartResponse(id)
if inIdle {
inIdle = false
}
switch req.typ {
case noIdle:
case commandListOk:
var ok, closed bool
ok = true
for _, args := range req.cmdList {
ok, closed = s.writeResponse(p, args, "list_OK")
if closed {
return
}
if !ok {
break
}
}
if ok {
p.PrintfLine("OK")
}
case simple:
if _, closed := s.writeResponse(p, req.args, "OK"); closed {
return
}
}
p.EndResponse(id)
}
}
示例7: runConnection
// run the mainloop for this connection
// stream if true means they support streaming mode
// reader if true means they support reader mode
func (self nntpConnection) runConnection(daemon NNTPDaemon, inbound, stream, reader bool, preferMode string, conn *textproto.Conn) {
var err error
var line string
var success bool
for err == nil {
if self.mode == "" {
if inbound {
// no mode and inbound
line, err = conn.ReadLine()
log.Println(self.name, line)
parts := strings.Split(line, " ")
cmd := parts[0]
if cmd == "CAPABILITIES" {
// write capabilities
conn.PrintfLine("101 i support to the following:")
dw := conn.DotWriter()
caps := []string{"VERSION 2", "READER", "STREAMING", "IMPLEMENTATION srndv2"}
for _, cap := range caps {
io.WriteString(dw, cap)
io.WriteString(dw, "\n")
}
dw.Close()
log.Println(self.name, "sent Capabilities")
} else if cmd == "MODE" {
if len(parts) == 2 {
if parts[1] == "READER" {
// set reader mode
self.mode = "READER"
// posting is not permitted with reader mode
conn.PrintfLine("201 Posting not permitted")
} else if parts[1] == "STREAM" {
// set streaming mode
conn.PrintfLine("203 Stream it brah")
self.mode = "STREAM"
log.Println(self.name, "streaming enabled")
go self.startStreaming(daemon, reader, conn)
}
}
} else {
// handle a it as a command, we don't have a mode set
parts := strings.Split(line, " ")
var code64 int64
code64, err = strconv.ParseInt(parts[0], 10, 32)
if err == nil {
err = self.handleLine(daemon, int(code64), line[4:], conn)
} else {
err = self.handleLine(daemon, 0, line, conn)
}
}
} else { // no mode and outbound
if preferMode == "stream" {
// try outbound streaming
if stream {
success, err = self.modeSwitch("STREAM", conn)
self.mode = "STREAM"
if success {
// start outbound streaming in background
go self.startStreaming(daemon, reader, conn)
}
}
} else if reader {
// try reader mode
success, err = self.modeSwitch("READER", conn)
if success {
self.mode = "READER"
self.startReader(daemon, conn)
}
}
if success {
log.Println(self.name, "mode set to", self.mode)
} else {
// bullshit
// we can't do anything so we quit
log.Println(self.name, "can't stream or read, wtf?")
conn.PrintfLine("QUIT")
conn.Close()
return
}
}
} else {
// we have our mode set
line, err = conn.ReadLine()
if err == nil {
parts := strings.Split(line, " ")
var code64 int64
code64, err = strconv.ParseInt(parts[0], 10, 32)
if err == nil {
err = self.handleLine(daemon, int(code64), line[4:], conn)
} else {
err = self.handleLine(daemon, 0, line, conn)
}
}
}
}
log.Println(self.name, "got error", err)
//.........这里部分代码省略.........
示例8: startReader
func (self nntpConnection) startReader(daemon NNTPDaemon, conn *textproto.Conn) {
log.Println(self.name, "run reader mode")
var err error
var code int
var line string
for err == nil {
// next article to ask for
msgid := <-self.article
log.Println(self.name, "asking for", msgid)
// send command
conn.PrintfLine("ARTICLE %s", msgid)
// read response
code, line, err = conn.ReadCodeLine(-1)
if code == 220 {
// awwww yeh we got it
var hdr textproto.MIMEHeader
// read header
hdr, err = conn.ReadMIMEHeader()
if err == nil {
// prepare to read body
dr := conn.DotReader()
// check header and decide if we want this
reason, err := self.checkMIMEHeader(daemon, hdr)
if err == nil {
if len(reason) > 0 {
log.Println(self.name, "discarding", msgid, reason)
// we don't want it, discard
io.Copy(ioutil.Discard, dr)
daemon.database.BanArticle(msgid, reason)
} else {
// yeh we want it open up a file to store it in
f := daemon.store.CreateTempFile(msgid)
if f == nil {
// already being loaded elsewhere
} else {
// write header to file
writeMIMEHeader(f, hdr)
// write article body to file
_, _ = io.Copy(f, dr)
// close file
f.Close()
log.Println(msgid, "obtained via reader from", self.name)
// tell daemon to load article via infeed
daemon.infeed_load <- msgid
}
}
} else {
// error happened while processing
log.Println(self.name, "error happend while processing MIME header", err)
}
} else {
// error happened while reading header
log.Println(self.name, "error happened while reading MIME header", err)
}
} else if code == 430 {
// they don't know it D:
log.Println(msgid, "not known by", self.name)
} else {
// invalid response
log.Println(self.name, "invald response to ARTICLE:", code, line)
}
}
// report error and close connection
log.Println(self.name, "error while in reader mode:", err)
conn.Close()
}
示例9: handleLine
func (self nntpConnection) handleLine(daemon NNTPDaemon, code int, line string, conn *textproto.Conn) (err error) {
parts := strings.Split(line, " ")
var msgid string
if code == 0 && len(parts) > 1 {
msgid = parts[1]
} else {
msgid = parts[0]
}
if code == 238 {
if ValidMessageID(msgid) {
log.Println("sending", msgid, "to", self.name)
// send the article to us
self.take <- msgid
}
} else if code == 239 {
// successful TAKETHIS
log.Println(msgid, "sent via", self.name)
// TODO: remember success
} else if code == 431 {
// CHECK said we would like this article later
log.Println("defer sending", msgid, "to", self.name)
go self.articleDefer(msgid)
} else if code == 439 {
// TAKETHIS failed
log.Println(msgid, "was not sent to", self.name, "denied:", line)
// TODO: remember denial
} else if code == 438 {
// they don't want the article
// TODO: remeber rejection
} else {
// handle command
parts := strings.Split(line, " ")
if len(parts) == 2 {
cmd := parts[0]
if cmd == "MODE" {
if parts[1] == "READER" {
// reader mode
self.mode = "READER"
log.Println(self.name, "switched to reader mode")
conn.PrintfLine("201 No posting Permitted")
} else if parts[1] == "STREAM" {
// wut? we're already in streaming mode
log.Println(self.name, "already in streaming mode")
conn.PrintfLine("203 Streaming enabled brah")
} else {
// invalid
log.Println(self.name, "got invalid mode request", parts[1])
conn.PrintfLine("501 invalid mode variant:", parts[1])
}
} else if cmd == "QUIT" {
// quit command
conn.PrintfLine("")
// close our connection and return
conn.Close()
return
} else if cmd == "CHECK" {
// handle check command
msgid := parts[1]
// have we seen this article?
if daemon.database.HasArticle(msgid) {
// yeh don't want it
conn.PrintfLine("438 %s", msgid)
} else if daemon.database.ArticleBanned(msgid) {
// it's banned we don't want it
conn.PrintfLine("438 %s", msgid)
} else {
// yes we do want it and we don't have it
conn.PrintfLine("238 %s", msgid)
}
} else if cmd == "TAKETHIS" {
// handle takethis command
var hdr textproto.MIMEHeader
var reason string
// read the article header
hdr, err = conn.ReadMIMEHeader()
if err == nil {
// check the header
reason, err = self.checkMIMEHeader(daemon, hdr)
dr := conn.DotReader()
if len(reason) > 0 {
// discard, we do not want
code = 439
log.Println(self.name, "rejected", msgid, reason)
_, err = io.Copy(ioutil.Discard, dr)
err = daemon.database.BanArticle(msgid, reason)
} else {
// check if we don't have the rootpost
reference := hdr.Get("References")
newsgroup := hdr.Get("Newsgroups")
if reference != "" && ValidMessageID(reference) && !daemon.store.HasArticle(reference) && !daemon.database.IsExpired(reference) {
log.Println(self.name, "got reply to", reference, "but we don't have it")
daemon.ask_for_article <- ArticleEntry{reference, newsgroup}
}
f := daemon.store.CreateTempFile(msgid)
if f == nil {
log.Println(self.name, "discarding", msgid, "we are already loading it")
// discard
io.Copy(ioutil.Discard, dr)
} else {
// write header
//.........这里部分代码省略.........
示例10: handleLine
func (self *nntpConnection) handleLine(daemon NNTPDaemon, code int, line string, conn *textproto.Conn) (err error) {
parts := strings.Split(line, " ")
var msgid string
if code == 0 && len(parts) > 1 {
msgid = parts[1]
} else {
msgid = parts[0]
}
if code == 238 {
if ValidMessageID(msgid) {
self.stream <- nntpTAKETHIS(msgid)
}
return
} else if code == 239 {
// successful TAKETHIS
log.Println(msgid, "sent via", self.name)
return
// TODO: remember success
} else if code == 431 {
// CHECK said we would like this article later
log.Println("defer sending", msgid, "to", self.name)
go self.articleDefer(msgid)
} else if code == 439 {
// TAKETHIS failed
log.Println(msgid, "was not sent to", self.name, "denied:", line)
// TODO: remember denial
} else if code == 438 {
// they don't want the article
// TODO: remeber rejection
} else {
// handle command
parts := strings.Split(line, " ")
if len(parts) > 1 {
cmd := parts[0]
if cmd == "MODE" {
if parts[1] == "READER" {
// reader mode
self.mode = "READER"
log.Println(self.name, "switched to reader mode")
conn.PrintfLine("201 No posting Permitted")
} else if parts[1] == "STREAM" {
// wut? we're already in streaming mode
log.Println(self.name, "already in streaming mode")
conn.PrintfLine("203 Streaming enabled brah")
} else {
// invalid
log.Println(self.name, "got invalid mode request", parts[1])
conn.PrintfLine("501 invalid mode variant:", parts[1])
}
} else if cmd == "QUIT" {
// quit command
conn.PrintfLine("")
// close our connection and return
conn.Close()
return
} else if cmd == "CHECK" {
// handle check command
msgid := parts[1]
// have we seen this article?
if daemon.database.HasArticle(msgid) {
// yeh don't want it
conn.PrintfLine("438 %s", msgid)
} else if daemon.database.ArticleBanned(msgid) {
// it's banned we don't want it
conn.PrintfLine("438 %s", msgid)
} else {
// yes we do want it and we don't have it
conn.PrintfLine("238 %s", msgid)
}
} else if cmd == "TAKETHIS" {
// handle takethis command
var hdr textproto.MIMEHeader
var reason string
// read the article header
hdr, err = conn.ReadMIMEHeader()
if err == nil {
// check the header
reason, err = self.checkMIMEHeader(daemon, hdr)
dr := conn.DotReader()
if len(reason) > 0 {
// discard, we do not want
code = 439
log.Println(self.name, "rejected", msgid, reason)
_, err = io.Copy(ioutil.Discard, dr)
err = daemon.database.BanArticle(msgid, reason)
} else {
// check if we don't have the rootpost
reference := hdr.Get("References")
newsgroup := hdr.Get("Newsgroups")
if reference != "" && ValidMessageID(reference) && !daemon.store.HasArticle(reference) && !daemon.database.IsExpired(reference) {
log.Println(self.name, "got reply to", reference, "but we don't have it")
daemon.ask_for_article <- ArticleEntry{reference, newsgroup}
}
f := daemon.store.CreateTempFile(msgid)
if f == nil {
log.Println(self.name, "discarding", msgid, "we are already loading it")
// discard
io.Copy(ioutil.Discard, dr)
} else {
// write header
//.........这里部分代码省略.........
示例11: handleLine
func (self *nntpConnection) handleLine(daemon *NNTPDaemon, code int, line string, conn *textproto.Conn) (err error) {
parts := strings.Split(line, " ")
var msgid string
if code == 0 && len(parts) > 1 {
msgid = parts[1]
} else {
msgid = parts[0]
}
if code == 238 {
if ValidMessageID(msgid) {
self.messageSetPendingState(msgid, "takethis")
// they want this article
self.takethis <- msgid
}
return
} else if code == 239 {
// successful TAKETHIS
log.Println(msgid, "sent via", self.name)
self.messageSetProcessed(msgid)
return
// TODO: remember success
} else if code == 431 {
// CHECK said we would like this article later
self.messageSetProcessed(msgid)
} else if code == 439 {
// TAKETHIS failed
log.Println(msgid, "was not sent to", self.name, "denied:", line)
self.messageSetProcessed(msgid)
// TODO: remember denial
} else if code == 438 {
// they don't want the article
// TODO: remeber rejection
self.messageSetProcessed(msgid)
} else {
// handle command
parts := strings.Split(line, " ")
if len(parts) > 1 {
cmd := strings.ToUpper(parts[0])
if cmd == "MODE" {
mode := strings.ToUpper(parts[1])
if mode == "READER" {
// reader mode
self.mode = "READER"
log.Println(self.name, "switched to reader mode")
if self.authenticated {
conn.PrintfLine("200 Posting Permitted")
} else {
conn.PrintfLine("201 No posting Permitted")
}
} else if mode == "STREAM" && self.authenticated {
// wut? we're already in streaming mode
log.Println(self.name, "already in streaming mode")
conn.PrintfLine("203 Streaming enabled brah")
} else {
// invalid
log.Println(self.name, "got invalid mode request", parts[1])
conn.PrintfLine("501 invalid mode variant:", parts[1])
}
} else if cmd == "QUIT" {
// quit command
conn.PrintfLine("")
// close our connection and return
conn.Close()
return
} else if cmd == "AUTHINFO" {
if len(parts) > 1 {
auth_cmd := strings.ToUpper(parts[1])
if auth_cmd == "USER" {
// first part
self.username = parts[2]
// next phase is PASS
conn.PrintfLine("381 Password required")
} else if auth_cmd == "PASS" {
if len(self.username) == 0 {
conn.PrintfLine("482 Authentication commands issued out of sequence")
} else {
// try login
var valid bool
valid, err = daemon.database.CheckNNTPUserExists(self.username)
if valid {
valid, err = daemon.database.CheckNNTPLogin(self.username, line[14:])
}
if valid {
// valid login
self.authenticated = true
conn.PrintfLine("281 Authentication accepted")
} else if err == nil {
// invalid login
conn.PrintfLine("481 Authentication rejected")
} else {
// there was an error
// logit
log.Println(self.name, "error while logging in as", self.username, err)
conn.PrintfLine("501 error while logging in")
}
}
}
} else {
// wut ?
//.........这里部分代码省略.........
示例12: outboundHandshake
// outbound setup, check capabilities and set mode
// returns (supports stream, supports reader, supports tls) + error
func (self *nntpConnection) outboundHandshake(conn *textproto.Conn, conf *FeedConfig) (stream, reader, tls bool, err error) {
log.Println(self.name, "outbound handshake")
var line string
var code int
for err == nil {
code, line, err = conn.ReadCodeLine(-1)
log.Println(self.name, line)
if err == nil {
if code == 200 {
// send capabilities
log.Println(self.name, "ask for capabilities")
err = conn.PrintfLine("CAPABILITIES")
if err == nil {
// read response
dr := conn.DotReader()
r := bufio.NewReader(dr)
for {
line, err = r.ReadString('\n')
if err == io.EOF {
// we are at the end of the dotreader
// set err back to nil and break out
err = nil
break
} else if err == nil {
if line == "STARTTLS\n" {
log.Println(self.name, "supports STARTTLS")
tls = true
} else if line == "MODE-READER\n" || line == "READER\n" {
log.Println(self.name, "supports READER")
reader = true
} else if line == "STREAMING\n" {
stream = true
log.Println(self.name, "supports STREAMING")
} else if line == "POSTIHAVESTREAMING\n" {
stream = true
reader = false
log.Println(self.name, "is SRNd")
}
} else {
// we got an error
log.Println("error reading capabilities", err)
break
}
}
// return after reading
break
}
} else if code == 201 {
log.Println("feed", self.name, "does not allow posting")
// we don't do auth yet
break
} else {
continue
}
}
}
if conf != nil && len(conf.username) > 0 && len(conf.passwd) > 0 {
log.Println(self.name, "authenticating...")
err = conn.PrintfLine("AUTHINFO USER %s", conf.username)
if err == nil {
var code int
code, line, err = conn.ReadCodeLine(381)
if code == 381 {
err = conn.PrintfLine("AUTHINFO PASS %s", conf.passwd)
if err == nil {
code, line, err = conn.ReadCodeLine(281)
if code == 281 {
log.Println(self.name, "Auth Successful")
} else {
log.Println(self.name, "Auth incorrect", line)
conn.PrintfLine("QUIT")
conn.Close()
return false, false, false, io.EOF
}
}
}
}
}
return
}
示例13: runConnection
// run the mainloop for this connection
// stream if true means they support streaming mode
// reader if true means they support reader mode
func (self *nntpConnection) runConnection(daemon *NNTPDaemon, inbound, stream, reader, use_tls bool, preferMode string, nconn net.Conn, conf *FeedConfig) {
self.addr = nconn.RemoteAddr()
var err error
var line string
var success bool
var conn *textproto.Conn
if (conf != nil && !conf.tls_off) && use_tls && daemon.CanTLS() && !inbound {
log.Println(self.name, "STARTTLS with", self.hostname)
_conn, state, err := SendStartTLS(nconn, daemon.GetTLSConfig(self.hostname))
if err == nil {
// we upgraded
conn = _conn
self.authenticated = state.HandshakeComplete
log.Println(self.name, "tls auth", self.authenticated)
} else {
log.Println(self.name, err)
// we didn't upgrade, fall back
conn = textproto.NewConn(nconn)
}
} else {
// we are authenticated if we are don't need tls
conn = textproto.NewConn(nconn)
}
if !inbound {
if preferMode == "stream" {
// try outbound streaming
if stream {
success, err = self.modeSwitch("STREAM", conn)
if success {
self.mode = "STREAM"
// start outbound streaming in background
go self.startStreaming(daemon, reader, conn)
}
}
} else if reader {
// try reader mode
success, err = self.modeSwitch("READER", conn)
if success {
self.mode = "READER"
self.startReader(daemon, conn)
return
}
}
if success {
log.Println(self.name, "mode set to", self.mode)
} else {
// bullshit
// we can't do anything so we quit
log.Println(self.name, "can't stream or read, wtf?")
conn.PrintfLine("QUIT")
conn.Close()
return
}
}
for err == nil {
line, err = conn.ReadLine()
if self.mode == "" {
if inbound {
if len(line) == 0 {
conn.Close()
return
} else if line == "QUIT" {
conn.PrintfLine("205 bai")
conn.Close()
return
}
parts := strings.Split(line, " ")
cmd := parts[0]
if cmd == "STARTTLS" {
_conn, state, err := HandleStartTLS(nconn, daemon.GetOurTLSConfig())
if err == nil {
// we are now tls
conn = _conn
self.tls_state = state
self.authenticated = state.HandshakeComplete
log.Println(self.name, "TLS initiated", self.authenticated)
} else {
log.Println("STARTTLS failed:", err)
}
} else if cmd == "CAPABILITIES" {
// write capabilities
conn.PrintfLine("101 i support to the following:")
dw := conn.DotWriter()
caps := []string{"VERSION 2", "READER", "STREAMING", "IMPLEMENTATION srndv2", "POST", "IHAVE", "AUTHINFO"}
if daemon.CanTLS() {
caps = append(caps, "STARTTLS")
}
for _, cap := range caps {
io.WriteString(dw, cap)
io.WriteString(dw, "\n")
}
dw.Close()
log.Println(self.name, "sent Capabilities")
} else if cmd == "MODE" {
if len(parts) == 2 {
//.........这里部分代码省略.........
示例14:
addr = fmt.Sprintf("%s:%d", host, port)
ok := netutils.Await(addr, 10*time.Millisecond, 2*time.Second)
Expect(ok).To(BeTrue())
clientConnect()
if variant.clientExplicitTls {
res("AUTH TLS")(234, "AUTH TLS successful.")
clientTlsUpgrade()
}
})
AfterEach(func() {
c.Close()
server.Close()
})
Describe("Commands", func() {
Describe("ABOR", func() {
It("ABOR", func() {
res("ABOR")(200, "OK")
})
})
Describe("ALLO", func() {
It("ALLO", func() {
res("ALLO")(202, "Obsolete")
})
})