V
V
Vladimir Grabko2016-04-26 14:34:02
go
Vladimir Grabko, 2016-04-26 14:34:02

How to set up password authentication?

I've been struggling for a couple of hours now (I don't understand how structures work well)
github.com/armon/go-socks5
I just want to set up login and password login. Judging by the lack of documentation, I can assume that a knowledgeable person will understand how to do this in a few minutes.
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2016-04-26
@lucifer-m

I guess that's how it should work.

package main

import (
  "github.com/armon/go-socks5"
)

func main() {

  conf := &socks5.Config{
    AuthMethods: []socks5.Authenticator{
      &socks5.UserPassAuthenticator{
        socks5.StaticCredentials{
          "username1": "password1",
          "username2": "password2",
          "username3": "password3",
        },
      },
    },
  }

  server, err := socks5.New(conf)
  if err != nil {
    panic(err)
  }

  // Create SOCKS5 proxy on localhost port 8000
  if err := server.ListenAndServe("tcp", "127.0.0.1:8000"); err != nil {
    panic(err)
  }
}

Let me explain why the author of the library did just that.
AuthMethods is an array so that you can set several authentication methods at the same time, if one fails, the next one from the array is tried.
Authenticator is an interface that allows you to substitute in its place all objects that have methods:
Authenticate(reader io.Reader, writer io.Writer) (*AuthContext, error)
and
GetCode() uint8
UserPassAuthenticator just implements these methods for the authentication case by name/password.
Inside it there is a Credentials field, into which you can substitute any object that satisfies the CredentialStore interface, i.e., has the Valid(user, password string) bool method. Accordingly, you can substitute your object here, to which the authenticator will throw a name / password pair, and the object will return in response whether this pair is valid or not.
The simplest implementation of such an object is in the module in the form of a StaticCredentials object, which is essentially a map[string]string and has a method that checks if the passed user/password pair is in this map.
That is, the module gives you a choice whether to use standard authentication implementations or write your own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question