FAQ
Hello.
I have a server that listen for incoming connections.
Server can accept ir reject connection based on source ip, but only in http
handler.
How can i reject incoming connection before http handler starts?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Minux at Mar 6, 2013 at 11:43 am

    On Wed, Mar 6, 2013 at 7:36 PM, Vasiliy Tolstov wrote:

    I have a server that listen for incoming connections.
    Server can accept ir reject connection based on source ip, but only in
    http handler.
    How can i reject incoming connection before http handler starts?
    you will need to provide a custom listener to net/http.(*Server).Serve.

    just wrap a standard net.Listener's Accept method with your filtering one.

    type filteringListener struct { net.Listener }
    func (fl filteringListener) Accept() (net.Conn, error) {
    c, err := fl.Listener.Accept()
    // fillter c
    return c, err
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Mar 6, 2013 at 2:28 pm

    2013/3/6 minux <minux.ma@gmail.com>:
    you will need to provide a custom listener to net/http.(*Server).Serve.

    just wrap a standard net.Listener's Accept method with your filtering one.

    type filteringListener struct { net.Listener }
    func (fl filteringListener) Accept() (net.Conn, error) {
    c, err := fl.Listener.Accept()
    // fillter c
    return c, err
    }
    My code now looks like this, does that mean , that i need to provide
    my own net.Listen
    function or i need to create variable with type filteringListener and
    with type assertion
    pass to it net.Listen listener ?

    func server(conf Conf) {
    r := http.NewServeMux()
    r.HandleFunc("/", process)
    http.Handle("/", r)

    s := &http.Server{
    Addr: ":80",
    Handler: r,
    ReadTimeout: 10 * time.Second,
    WriteTimeout: 10 * time.Second,
    MaxHeaderBytes: 1 << 20,
    }

    l, err := net.Listen("tcp", ":80")
    if err != nil {
    conf.Log.Panicf(err.Error())
    }
    go s.Serve(l)
    select {
    case <-conf.ServerClose:
    conf.Log.Printf("Server thread exit")
    l.Close()
    return
    break
    }
    }


    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Mar 12, 2013 at 7:38 am

    2013/3/6 minux <minux.ma@gmail.com>:
    you will need to provide a custom listener to net/http.(*Server).Serve.

    just wrap a standard net.Listener's Accept method with your filtering one.

    type filteringListener struct { net.Listener }
    func (fl filteringListener) Accept() (net.Conn, error) {
    c, err := fl.Listener.Accept()
    // fillter c
    return c, err
    }

    Sorry, i'm not understand, how can i use this filteringListener in
    net/http serve?

    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Mar 12, 2013 at 11:51 am

    On Tue, Mar 12, 2013 at 3:38 PM, Vasiliy Tolstov wrote:
    2013/3/6 minux <minux.ma@gmail.com>:
    you will need to provide a custom listener to net/http.(*Server).Serve.

    just wrap a standard net.Listener's Accept method with your filtering one.

    type filteringListener struct { net.Listener }
    func (fl filteringListener) Accept() (net.Conn, error) {
    c, err := fl.Listener.Accept()
    // fillter c
    return c, err
    }

    Sorry, i'm not understand, how can i use this filteringListener in
    net/http serve?
    filteringListen still implements net.Listener interface, so it can be
    passed to http.Serve.

    you might want to read about struct/interface embedding in Effective Go:
    http://golang.org/doc/effective_go.html#embedding

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Tolstov at Mar 13, 2013 at 2:49 pm

    2013/3/12 minux <minux.ma@gmail.com>:
    filteringListen still implements net.Listener interface, so it can be
    passed to http.Serve.

    you might want to read about struct/interface embedding in Effective Go:
    http://golang.org/doc/effective_go.html#embedding

    Thanks. I'm reread docs. Now all fine =).
    Last question - how can i properly close unwanted connection and
    retunr from Accept?
    I'm write this code, but does this doing right thinkgs or i need to
    close only CloseRead?

    func (acl aclListener) Accept() (net.Conn, error) {
    c, err := acl.Listener.Accept()
    h, _, _ := net.SplitHostPort(c.RemoteAddr().String())
    fmt.Printf("%s\n", h)
    switch h {
    "127.0.0.1":
    break
    default:
    c.Close()
    return c, errors.New("EACCESS")
    break
    }
    // fillter c
    return c, err
    }


    --
    Vasiliy Tolstov,
    e-mail: v.tolstov@selfip.ru
    jabber: vase@selfip.ru

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 6, '13 at 11:36a
activeMar 13, '13 at 2:49p
posts6
users2
websitegolang.org

2 users in discussion

Vasiliy Tolstov: 4 posts Minux: 2 posts

People

Translate

site design / logo © 2022 Grokbase