All Projects → huandu → Go Tls

huandu / Go Tls

Licence: other
A bit safer approach to implement Thread Local Storage (TLS) for Go 1.7+.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects
hack
652 projects

Projects that are alternatives of or similar to Go Tls

Docker Ssllabs Scan
Qualys sslabs-scan utility in a tiny docker image
Stars: ✭ 85 (-18.27%)
Mutual labels:  tls
Ssl Checker
Python script that collects SSL/TLS information from hosts
Stars: ✭ 94 (-9.62%)
Mutual labels:  tls
Koa Sslify
Enforce HTTPS in node.js koa apps
Stars: ✭ 100 (-3.85%)
Mutual labels:  tls
Voyager
🚀 Secure HAProxy Ingress Controller for Kubernetes
Stars: ✭ 1,276 (+1126.92%)
Mutual labels:  tls
Tls Inspector
Easily view and inspect X.509 certificates on your iOS device.
Stars: ✭ 92 (-11.54%)
Mutual labels:  tls
Minimalftp
A lightweight, simple FTP server. Pure Java, no dependencies.
Stars: ✭ 94 (-9.62%)
Mutual labels:  tls
Tlstunnel Lwt
TLS tunnel -- an alternative to stud / stunnel
Stars: ✭ 79 (-24.04%)
Mutual labels:  tls
Gcp Iot Core Examples
Google Cloud Platform IOT Core Examples
Stars: ✭ 103 (-0.96%)
Mutual labels:  tls
Php Hyper Builtin Server
Reverse proxy for PHP built-in server which supports multiprocessing and TLS/SSL encryption
Stars: ✭ 93 (-10.58%)
Mutual labels:  tls
Essential Youtube
An experimental UI for YouTube.
Stars: ✭ 99 (-4.81%)
Mutual labels:  experimental
Wasps
wasps is a lightweight goroutine pool for golang, use limited goroutines to achieve multi-task concurrent execution.
Stars: ✭ 88 (-15.38%)
Mutual labels:  goroutine
Ghostunnel
A simple SSL/TLS proxy with mutual authentication for securing non-TLS services
Stars: ✭ 1,296 (+1146.15%)
Mutual labels:  tls
Dotweb
Simple and easy go web micro framework
Stars: ✭ 1,354 (+1201.92%)
Mutual labels:  tls
Base Drafts
Internet-Drafts that make up the base QUIC specification
Stars: ✭ 1,270 (+1121.15%)
Mutual labels:  tls
Sslsplit
Transparent SSL/TLS interception
Stars: ✭ 1,371 (+1218.27%)
Mutual labels:  tls
Go Concurrency
This repos has lots of Go concurrency, goroutine and channel usage and best practice examples
Stars: ✭ 84 (-19.23%)
Mutual labels:  goroutine
Nitmproxy
Proxy server based on netty
Stars: ✭ 94 (-9.62%)
Mutual labels:  tls
Nonce Disrespect
Nonce-Disrespecting Adversaries: Practical Forgery Attacks on GCM in TLS
Stars: ✭ 103 (-0.96%)
Mutual labels:  tls
Websocks
A secure proxy based on WebSocket. 一个基于 WebSocket 的代理工具
Stars: ✭ 102 (-1.92%)
Mutual labels:  tls
Gsocks5
Secure SOCKS5 server in Go
Stars: ✭ 98 (-5.77%)
Mutual labels:  tls

go-tls: TLS for any goroutine

CircleCI GoDoc

WARNING: It's not recommended to use this package in any production environment. It may crash you at any time. Use context instead when possible.

Package tls provides TLS for any goroutine by hijacking runtime.goexit on stack. Comparing with other similar packages, this package avoids any potential resource leak in TLS.

Install

Use go get to install this package.

    go get github.com/huandu/go-tls

Use TLS

Set arbitrary data and get it later.

k := "my key"
v := 1234
tls.Set(k, tls.MakeData(v))

// Get data by k.
d, ok := tls.Get(k)
assert(ok)
assert(d.Value().(int) == v)

// Get a unique ID for current goroutine.
// It's guaranteed to be unique.
id := tls.ID()

// Delete data by k.
tls.Del(k)

// Reset TLS so that all keys are removed and all data is closed if necessary.
// It doesn't remove any AtExit handler.
tls.Reset()

// Completely unload TLS and discard all data and AtExit handlers.
// If TLS method is called after Unload, a new TLS stub will be created.
// The ID() will return a different value.
tls.Unload()

If the data implements io.Closer, it will be called automatically when Reset is called or goroutine exits. It's not allowed to use any TLS methods in the Close method of TLS data. It will cause permanent memory leak.

Execute code when goroutine exits

AtExit pushes a function to a slice of at-exit handlers and executes them when goroutine is exiting in FILO order. All TLS data is still available when calling at-exit handlers.

AtExit doesn't work on main goroutine as it doesn't exit at all.

tls.AtExit(func() {
    // Do something when goroutine is exiting...
})

Limitations

Several limitations so far.

  • Works with Go 1.7 or newer.
  • AtExit doesn't work on main goroutine, as this goroutine always exits with os.Exit(0) instead of calling goexit. See main() in src/runtime/proc.go.

How it works

It's quite a long story I don't have time to write everything down right now.

TL; DR. Package tls uses goroutine's g struct pointer to identify a goroutine and hacks runtime.goexit to do house clean work when goroutine exits.

This approach is relatively safe, because all technics are based on runtime types which doesn't change since Go1.0.

Following runtime types are used.

  • The g.stack: It's the first field of g. It stores stack memory range of a g.
  • Function symbol table: When Go runtime allocates more stack, it validates all return addresses on stack. If I change runtime.goexit to another function pc, runtime will complain it as it's not a valid top of stack function (checked by runtime.topofstack). As a workaround, I hacks function symbol table to set _func.pcsp of the hacked goexit to 0 to skip checks.

Similar packages

  • github.com/jtolds/gls: Goroutine local storage on current goroutine's stack. We must start goroutines with Go func explicitly before using any context methods.
  • github.com/v2pro/plz/gls: Use goid as a unique key for any goroutine and store contextual information.

License

This package is licensed under MIT license. See LICENSE for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].