Network Programming with Go
jan.newmarch.name
jan.newmarch.name
How password should be encrypted? How session id should be generated and passed to the user? How session information should be saved? (cookie? file? database? memory?)
Also, I'd be interested in https, from theory to implementation.
How password should be encrypted?
Choose your package:
go get github.com/dchest/scrypt
go get code.google.com/p/go.crypto/bcrypt
go get code.google.com/p/go.crypto/pbkdf2
How session id should be generated and passed to the user?If you mean how you can authenticate logged in users, then you can use this package:
go get github.com/dchest/authcookie
How session information should be saved? (cookie? file? database? memory?)Up to you, but generally it's better to avoid cookies for storing anything other than IDs. There are a few packages for "sessions" like in PHP, but I don't remember their names.
One more important question to add:
How do I protect against XSRF attacks?
go get code.google.com/p/xsrftoken
You can read documentation for these packages here:
http://go.pkgdoc.org/As for HTTPS, it's a complicated thing. Pick any good book on it, then apply your knowledge to the standard package crypto/tls (http://golang.org/pkg/crypto/tls/).
Due to Go's pervasive use of io.Writer / io.Reader / net.Conn interfaces, once you have the problem of loading keys sorted, everything else is the same as writing a plain old TCP/IP listener. Configuring TLS will always be a pain -- decades, committees and broken implementations have seen to that.
As for actually verifying passwords and session integrity, Go's crypto packages are also pretty straightforwards and composable. The crypto/hmac [4] package is easily combined with various implementations of hash.Hash. In exchange for this simplicity, though, Go gives you plenty of opportunity to emulate LinkedIn and blow your own foot off, forgetting salt and common sense. There are plenty of higher level packages that offer to do it for you. (Blow your foot off, or hash your passwords -- I recommend reading implementations to decide which. :))
Also of interest when dealing with writing webapps in Go, the database/sql [5] package is an important abstraction. Since it is a more recent package, it often doesn't show up in google searches for sqlite, postgres, etc. This can give an impression that Go's database support is fragmented.
1 -- http://golang.org/pkg/net/http/#ListenAndServeTLS
2 -- http://golang.org/pkg/net/http/#Transport
3 -- http://golang.org/pkg/crypto/tls/#Listener
For network programming I've often used Python + Twisted + ZMQ, what are the most commonly used alternatives?
Answering your second question, almost every good programming language has some good networking libraries. Choice of language usually depends on what you are building and what are your requirements.
Last week I also developed a little random password generating web server with a web service API in Go. It's live at http://random-password-please.com/, and the source is at https://github.com/jbarham/random-password-please. IMHO it's a good illustration of how easy it is to write network servers in Go as it makes use of goroutines and channels and it all fits together very easily and naturally.
Using Go in a professional setting really isn't a technical challenge. The biggest concern is if you have to grow your team, you're either going to have to teach Go (which isn't that hard -- I've seen two different people come from dynamic languages and pick the basics up in a week) or hire people who know Go (often already happily employed).
To further mangle Charles V: "I speak Spanish to God, German to my horse, and Go to connections that might make me wait."
It was quite disturbing that firing off several thousand goroutines resulted in several thousand native threads - resulting the system trashing. If I inserted a very small pause between creating the go routines, this did not occur and just 1 or 2 native threads were created. I tried the runtime.GOMAXPROCS, to no awail. Is there any way of controlling this kind of behavior ?
You can do one of:
1. limit thread creation by using your operating systems limiting features. eg ulimit
2. rebuild the standard lib with DISABLE_CGO=1 to avoid the calls in to C if that's possible in your environment.
3. batch all your dns lookups in to a single goroutine.
4. make changes to the operating system so provide async dns lookups.
The whole goroutines and channels mechanism is fascinating. I've seen and used coroutines in C that in a way achieve what goroutines do but the simplicity and the combination with channels is really great.
Comparing a co-routine based network server in C and Go would be the way to go to see the real strength of Go, IMNHO.