今天在 GitHub Trending 上看到一个有意思的项目: tailscale,这是一个开源项目

一、项目概述

ThisrepositorycontainsthemajorityofTailscale’sopensourcecode. Notably,itincludesthetailscaleddaemonand thetailscaleCLItool.ThetailscaleddaemonrunsonLinux,Windows, ,andtovaryingdegrees onFreeBSDandOpenBSD.TheTailscaleiOSandAndroidappsusethisrepo’s code,butthisrepodoesn’tcontainthemobileGUIcode.

GitHub: https://github.com/tailscale/tailscale

二、核心特性

  • Docker 支持,开箱即用
  • 标准包管理,依赖安装简单
  • 含测试用例,质量有保障
  • README 文档完善,上手容易

三、技术实现

项目基于以下关键技术实现:

核心文件结构

文件说明
Dockerfile· 3.0 KB
Makefile· 10.7 KB
assert_ts_toolchain_match.goGO · 1.1 KB
cache_key_test.goGO · 1.7 KB
go.modMOD · 26.5 KB
gomod_test.goGO · 0.5 KB
license_test.goGO · 2.7 KB
pkgdoc_test.goGO · 2.3 KB

核心代码示例

Dockerfile:

# Copyright (c) Tailscale Inc & contributors
# SPDX-License-Identifier: BSD-3-Clause

# Note that this Dockerfile is currently NOT used to build any of the published
# Tailscale container images and may have drifted from the image build mechanism
# we use.
# Tailscale images are currently built using https://github.com/tailscale/mkctr,
# and the build script can be found in ./build_docker.sh.
#
# If you want to build local images for testing, you can use make.
#
# To build a Tailscale image and push to the local docker registry:
#
#   $ REPO=local/tailscale TAGS=v0.0.1 PLATFORM=local  make publishdevimage
#
# To build a Tailscale image and push to a remote docker registry:
#
#   $ REPO=<your-registry>/<your-repo>/tailscale TAGS=v0.0.1  make publishdevimage
#
# This Dockerfile includes all the tailscale binaries.
#
# To build the Dockerfile:
#
#     $ docker build -t tailscale/tailscale .
#
# To run the tailscaled agent:
#
#     $ docker run -d --name=tailscaled -v /var/lib:/var/lib -v /dev/net/tun:/dev/net/tun --network=host --privileged tailscale/tailscale tailscaled
#
# To then log in:

Makefile:

IMAGE_REPO ?= tailscale/tailscale
SYNO_ARCH ?= "x86_64"
SYNO_DSM ?= "7"
TAGS ?= "latest"

PLATFORM ?= "flyio" ## flyio==linux/amd64. Set to "" to build all platforms.

vet: ## Run go vet
	./tool/go vet ./...

tidy: ## Run go mod tidy and update nix flake hashes
	./tool/go mod tidy
	./tool/go run ./tool/updateflakes

lint: ## Run golangci-lint
	./tool/go run github.com/golangci/golangci-lint/cmd/golangci-lint run

updatedeps: ## Update depaware deps
	# depaware (via x/tools/go/packages) shells back to "go", so make sure the "go"
	# it finds in its $$PATH is the right one.
	PATH="$$(./tool/go env GOROOT)/bin:$$PATH" ./tool/go run github.com/tailscale/depaware --update --vendor --internal \
		tailscale.com/cmd/tailscaled \
		tailscale.com/cmd/tailscale \
		tailscale.com/cmd/derper \
		tailscale.com/cmd/k8s-operator \
		tailscale.com/cmd/stund \
		tailscale.com/cmd/tsidp
	PATH="$$(./tool/go env GOROOT)/bin:$$PATH" ./tool/go run github.com/tailscale/depaware --update --goos=linux,darwin,windows,android,ios --vendor --internal \
		tailscale.com/tsnet
	PATH="$$(./tool/go env GOROOT)/bin:$$PATH" ./tool/go run github.com/tailscale/depaware --update --file=depaware-minbox.txt --goos=linux --tags="$$(./tool/go run ./cmd/featuretags --min --add=cli)" --vendor --internal \

assert_ts_toolchain_match.go:

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

//go:build tailscale_go

package tailscaleroot

import (
	"fmt"
	"os"
	"strings"
)

func init() {
	tsRev, ok := tailscaleToolchainRev()
	if !ok {
		panic("binary built with tailscale_go build tag but failed to read build info or find tailscale.toolchain.rev in build info")
	}
	want := strings.TrimSpace(GoToolchainRev)
	// Also permit the "next" toolchain rev, which is used in the main branch and will eventually become the new "current" rev.
	// This allows building with TS_GO_NEXT=1 and then running the resulting binary without TS_GO_NEXT=1.
	wantAlt := strings.TrimSpace(GoToolchainNextRev)
	if tsRev != want && tsRev != wantAlt {
		if os.Getenv("TS_PERMIT_TOOLCHAIN_MISMATCH") == "1" {
			fmt.Fprintf(os.Stderr, "tailscale.toolchain.rev = %q, want %q; but ignoring due to TS_PERMIT_TOOLCHAIN_MISMATCH=1\n", tsRev, want)
			return
		}
		panic(fmt.Sprintf("binary built with tailscale_go build tag but Go toolchain %q doesn't match github.com/tailscale/tailscale expected value %q; override this failure with TS_PERMIT_TOOLCHAIN_MISMATCH=1", tsRev, want))
	}
}

四、快速开始

go install tailscale.com/cmd/tailscale{,d}

五、适用场景

  • 开发者研究新技术栈和最佳实践
  • 项目快速启动和原型开发
  • 学习开源项目的设计思路和架构
  • 集成到现有项目中作为依赖

六、总结

tailscale 是 GitHub Trending 上的热门开源项目, 在技术社区具有较高影响力。 项目代码结构清晰,文档完善,适合深入学习和实际应用。

🔗 项目地址:https://github.com/tailscale/tailscale