Golang 防火墙编程

非编程方式 Windows 使用netsh方式进行防火墙规则的维护 Linux 编程方式 Windows Tailscale开发了一个应用WFP的库inet.af/wf 。参考参考链接。在tailscale中的封装代码如下(源链接 ): // Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause //go:build windows package wf import ( "fmt" "net/netip" "os" "golang.org/x/sys/windows" "inet.af/wf" "tailscale.com/net/netaddr" ) // Known addresses. var ( linkLocalRange = netip.MustParsePrefix("ff80::/10") linkLocalDHCPMulticast = netip.MustParseAddr("ff02::1:2") siteLocalDHCPMulticast = netip.MustParseAddr("ff05::1:3") linkLocalRouterMulticast = netip.MustParseAddr("ff02::2") ) type direction int const ( directionInbound direction = iota directionOutbound directionBoth ) type protocol int const ( protocolV4 protocol = iota protocolV6 protocolAll ) // getLayers returns the wf.LayerIDs where the rules should be added based // on the protocol and direction. func (p protocol) getLayers(d direction) []wf.LayerID { var layers []wf.LayerID if p == protocolAll || p == protocolV4 { if d == directionBoth || d == directionInbound { layers = append(layers, wf.LayerALEAuthRecvAcceptV4) } if d == directionBoth || d == directionOutbound { layers = append(layers, wf.LayerALEAuthConnectV4) } } if p == protocolAll || p == protocolV6 { if d == directionBoth || d == directionInbound { layers = append(layers, wf.LayerALEAuthRecvAcceptV6) } if d == directionBoth || d == directionOutbound { layers = append(layers, wf.LayerALEAuthConnectV6) } } return layers } func ruleName(action wf.Action, l wf.LayerID, name string) string { switch l { case wf.LayerALEAuthConnectV4: return fmt.Sprintf("%s outbound %s (IPv4)", action, name) case wf.LayerALEAuthConnectV6: return fmt.Sprintf("%s outbound %s (IPv6)", action, name) case wf.LayerALEAuthRecvAcceptV4: return fmt.Sprintf("%s inbound %s (IPv4)", action, name) case wf.LayerALEAuthRecvAcceptV6: return fmt.Sprintf("%s inbound %s (IPv6)", action, name) } return "" } // Firewall uses the Windows Filtering Platform to implement a network firewall. type Firewall struct { luid uint64 providerID wf.ProviderID sublayerID wf.SublayerID session *wf.Session permittedRoutes map[netip.Prefix][]*wf.Rule } // New returns a new Firewall for the provided interface ID. func New(luid uint64) (*Firewall, error) { session, err := wf.New(&wf.Options{ Name: "Tailscale firewall", Dynamic: true, }) if err != nil { return nil, err } wguid, err := windows.GenerateGUID() if err != nil { return nil, err } providerID := wf.ProviderID(wguid) if err := session.AddProvider(&wf.Provider{ ID: providerID, Name: "Tailscale provider", }); err != nil { return nil, err } wguid, err = windows.GenerateGUID() if err != nil { return nil, err } sublayerID := wf.SublayerID(wguid) if err := session.AddSublayer(&wf.Sublayer{ ID: sublayerID, Name: "Tailscale permissive and blocking filters", Weight: 0, }); err != nil { return nil, err } f := &Firewall{ luid: luid, session: session, providerID: providerID, sublayerID: sublayerID, permittedRoutes: make(map[netip.Prefix][]*wf.Rule), } if err := f.enable(); err != nil { return nil, err } return f, nil } type weight uint64 const ( weightTailscaleTraffic weight = 15 weightKnownTraffic weight = 12 weightCatchAll weight = 0 ) func (f *Firewall) enable() error { if err := f.permitTailscaleService(weightTailscaleTraffic); err != nil { return fmt.Errorf("permitTailscaleService failed: %w", err) } if err := f.permitTunInterface(weightTailscaleTraffic); err != nil { return fmt.Errorf("permitTunInterface failed: %w", err) } if err := f.permitDNS(weightTailscaleTraffic); err != nil { return fmt.Errorf("permitDNS failed: %w", err) } if err := f.permitLoopback(weightTailscaleTraffic); err != nil { return fmt.Errorf("permitLoopback failed: %w", err) } if err := f.permitDHCPv4(weightKnownTraffic); err != nil { return fmt.Errorf("permitDHCPv4 failed: %w", err) } if err := f.permitDHCPv6(weightKnownTraffic); err != nil { return fmt.Errorf("permitDHCPv6 failed: %w", err) } if err := f.permitNDP(weightKnownTraffic); err != nil { return fmt.Errorf("permitNDP failed: %w", err) } /* TODO: actually evaluate if this does anything and if we need this. It's layer 2; our other rules are layer 3. * In other words, if somebody complains, try enabling it. For now, keep it off. * TODO(maisem): implement this. err = permitHyperV(session, baseObjects, weightKnownTraffic) if err != nil { return wrapErr(err) } */ if err := f.blockAll(weightCatchAll); err != nil { return fmt.Errorf("blockAll failed: %w", err) } return nil } // UpdatedPermittedRoutes adds rules to allow incoming and outgoing connections // from the provided prefixes. It will also remove rules for routes that were // previously added but have been removed. func (f *Firewall) UpdatePermittedRoutes(newRoutes []netip.Prefix) error { var routesToAdd []netip.Prefix routeMap := make(map[netip.Prefix]bool) for _, r := range newRoutes { routeMap[r] = true if _, ok := f.permittedRoutes[r]; !ok { routesToAdd = append(routesToAdd, r) } } var routesToRemove []netip.Prefix for r := range f.permittedRoutes { if !routeMap[r] { routesToRemove = append(routesToRemove, r) } } for _, r := range routesToRemove { for _, rule := range f.permittedRoutes[r] { if err := f.session.DeleteRule(rule.ID); err != nil { return err } } delete(f.permittedRoutes, r) } for _, r := range routesToAdd { conditions := []*wf.Match{ { Field: wf.FieldIPRemoteAddress, Op: wf.MatchTypeEqual, Value: r, }, } var p protocol if r.Addr().Is4() { p = protocolV4 } else { p = protocolV6 } rules, err := f.addRules("local route", weightKnownTraffic, conditions, wf.ActionPermit, p, directionBoth) if err != nil { return err } f.permittedRoutes[r] = rules } return nil } func (f *Firewall) newRule(name string, w weight, layer wf.LayerID, conditions []*wf.Match, action wf.Action) (*wf.Rule, error) { id, err := windows.GenerateGUID() if err != nil { return nil, err } return &wf.Rule{ Name: ruleName(action, layer, name), ID: wf.RuleID(id), Provider: f.providerID, Sublayer: f.sublayerID, Layer: layer, Weight: uint64(w), Conditions: conditions, Action: action, }, nil } func (f *Firewall) addRules(name string, w weight, conditions []*wf.Match, action wf.Action, p protocol, d direction) ([]*wf.Rule, error) { var rules []*wf.Rule for _, l := range p.getLayers(d) { r, err := f.newRule(name, w, l, conditions, action) if err != nil { return nil, err } if err := f.session.AddRule(r); err != nil { return nil, err } rules = append(rules, r) } return rules, nil } func (f *Firewall) blockAll(w weight) error { _, err := f.addRules("all", w, nil, wf.ActionBlock, protocolAll, directionBoth) return err } func (f *Firewall) permitNDP(w weight) error { // These are aliased according to: // https://social.msdn.microsoft.com/Forums/azure/en-US/eb2aa3cd-5f1c-4461-af86-61e7d43ccc23/filtering-icmp-by-type-code?forum=wfp fieldICMPType := wf.FieldIPLocalPort fieldICMPCode := wf.FieldIPRemotePort var icmpConditions = func(t, c uint16, remoteAddress any) []*wf.Match { conditions := []*wf.Match{ { Field: wf.FieldIPProtocol, Op: wf.MatchTypeEqual, Value: wf.IPProtoICMPV6, }, { Field: fieldICMPType, Op: wf.MatchTypeEqual, Value: t, }, { Field: fieldICMPCode, Op: wf.MatchTypeEqual, Value: c, }, } if remoteAddress != nil { conditions = append(conditions, &wf.Match{ Field: wf.FieldIPRemoteAddress, Op: wf.MatchTypeEqual, Value: linkLocalRouterMulticast, }) } return conditions } /* TODO: actually handle the hop limit somehow! The rules should vaguely be: * - icmpv6 133: must be outgoing, dst must be FF02::2/128, hop limit must be 255 * - icmpv6 134: must be incoming, src must be FE80::/10, hop limit must be 255 * - icmpv6 135: either incoming or outgoing, hop limit must be 255 * - icmpv6 136: either incoming or outgoing, hop limit must be 255 * - icmpv6 137: must be incoming, src must be FE80::/10, hop limit must be 255 */ // // Router Solicitation Message // ICMP type 133, code 0. Outgoing. // conditions := icmpConditions(133, 0, linkLocalRouterMulticast) if _, err := f.addRules("NDP type 133", w, conditions, wf.ActionPermit, protocolV6, directionOutbound); err != nil { return err } // // Router Advertisement Message // ICMP type 134, code 0. Incoming. // conditions = icmpConditions(134, 0, linkLocalRange) if _, err := f.addRules("NDP type 134", w, conditions, wf.ActionPermit, protocolV6, directionInbound); err != nil { return err } // // Neighbor Solicitation Message // ICMP type 135, code 0. Bi-directional. // conditions = icmpConditions(135, 0, nil) if _, err := f.addRules("NDP type 135", w, conditions, wf.ActionPermit, protocolV6, directionBoth); err != nil { return err } // // Neighbor Advertisement Message // ICMP type 136, code 0. Bi-directional. // conditions = icmpConditions(136, 0, nil) if _, err := f.addRules("NDP type 136", w, conditions, wf.ActionPermit, protocolV6, directionBoth); err != nil { return err } // // Redirect Message // ICMP type 137, code 0. Incoming. // conditions = icmpConditions(137, 0, linkLocalRange) if _, err := f.addRules("NDP type 137", w, conditions, wf.ActionPermit, protocolV6, directionInbound); err != nil { return err } return nil } func (f *Firewall) permitDHCPv6(w weight) error { var dhcpConditions = func(remoteAddrs ...any) []*wf.Match { conditions := []*wf.Match{ { Field: wf.FieldIPProtocol, Op: wf.MatchTypeEqual, Value: wf.IPProtoUDP, }, { Field: wf.FieldIPLocalAddress, Op: wf.MatchTypeEqual, Value: linkLocalRange, }, { Field: wf.FieldIPLocalPort, Op: wf.MatchTypeEqual, Value: uint16(546), }, { Field: wf.FieldIPRemotePort, Op: wf.MatchTypeEqual, Value: uint16(547), }, } for _, a := range remoteAddrs { conditions = append(conditions, &wf.Match{ Field: wf.FieldIPRemoteAddress, Op: wf.MatchTypeEqual, Value: a, }) } return conditions } conditions := dhcpConditions(linkLocalDHCPMulticast, siteLocalDHCPMulticast) if _, err := f.addRules("DHCP request", w, conditions, wf.ActionPermit, protocolV6, directionOutbound); err != nil { return err } conditions = dhcpConditions(linkLocalRange) if _, err := f.addRules("DHCP response", w, conditions, wf.ActionPermit, protocolV6, directionInbound); err != nil { return err } return nil } func (f *Firewall) permitDHCPv4(w weight) error { var dhcpConditions = func(remoteAddrs ...any) []*wf.Match { conditions := []*wf.Match{ { Field: wf.FieldIPProtocol, Op: wf.MatchTypeEqual, Value: wf.IPProtoUDP, }, { Field: wf.FieldIPLocalPort, Op: wf.MatchTypeEqual, Value: uint16(68), }, { Field: wf.FieldIPRemotePort, Op: wf.MatchTypeEqual, Value: uint16(67), }, } for _, a := range remoteAddrs { conditions = append(conditions, &wf.Match{ Field: wf.FieldIPRemoteAddress, Op: wf.MatchTypeEqual, Value: a, }) } return conditions } conditions := dhcpConditions(netaddr.IPv4(255, 255, 255, 255)) if _, err := f.addRules("DHCP request", w, conditions, wf.ActionPermit, protocolV4, directionOutbound); err != nil { return err } conditions = dhcpConditions() if _, err := f.addRules("DHCP response", w, conditions, wf.ActionPermit, protocolV4, directionInbound); err != nil { return err } return nil } func (f *Firewall) permitTunInterface(w weight) error { condition := []*wf.Match{ { Field: wf.FieldIPLocalInterface, Op: wf.MatchTypeEqual, Value: f.luid, }, } _, err := f.addRules("on TUN", w, condition, wf.ActionPermit, protocolAll, directionBoth) return err } func (f *Firewall) permitLoopback(w weight) error { condition := []*wf.Match{ { Field: wf.FieldFlags, Op: wf.MatchTypeFlagsAllSet, Value: wf.ConditionFlagIsLoopback, }, } _, err := f.addRules("on loopback", w, condition, wf.ActionPermit, protocolAll, directionBoth) return err } func (f *Firewall) permitDNS(w weight) error { conditions := []*wf.Match{ { Field: wf.FieldIPRemotePort, Op: wf.MatchTypeEqual, Value: uint16(53), }, // Repeat the condition type for logical OR. { Field: wf.FieldIPProtocol, Op: wf.MatchTypeEqual, Value: wf.IPProtoUDP, }, { Field: wf.FieldIPProtocol, Op: wf.MatchTypeEqual, Value: wf.IPProtoTCP, }, } _, err := f.addRules("DNS", w, conditions, wf.ActionPermit, protocolAll, directionBoth) return err } func (f *Firewall) permitTailscaleService(w weight) error { currentFile, err := os.Executable() if err != nil { return err } appID, err := wf.AppID(currentFile) if err != nil { return fmt.Errorf("could not get app id for %q: %w", currentFile, err) } conditions := []*wf.Match{ { Field: wf.FieldALEAppID, Op: wf.MatchTypeEqual, Value: appID, }, } _, err = f.addRules("unrestricted traffic for Tailscale service", w, conditions, wf.ActionPermit, protocolAll, directionBoth) return err } Linux Tailscale的Linux版本实现 https://github.com/tailscale/tailscale/blob/main/util/linuxfw/linuxfw.go ...

2023-07-03 · 8 分钟 · czyt

使用aws go-sdk访问cloudflare R2文件

准备 需要准备Cloudflare的accountId,相应的R2 ak sk 和bucketName等信息. var ( accountId = "xxxxx" accessKeyId = "cbdade718b2ca877882csssssfcf" accessKeySecret = "04917c7d745422022e266f6b06" bucketName = "gopher" ) 完整代码 package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/service/s3" "log" ) func main() { var ( accountId = "xxxxx" accessKeyId = "cbdade718b2ca877882csssssfcf" accessKeySecret = "04917c7d745422022e266f6b06" bucketName = "gopher" ) var r2Resolver = aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { return aws.Endpoint{ URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountId), }, nil }) cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolverWithOptions(r2Resolver), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")), ) if err != nil { log.Fatal(err) } client := s3.NewFromConfig(cfg) listObjectsOutput, err := client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{ Bucket: &bucketName, }) if err != nil { log.Fatal(err) } presignClient := s3.NewPresignClient(client) for _, object := range listObjectsOutput.Contents { // 创建文件访问链接 presignResult, err := presignClient.PresignGetObject(context.TODO(), &s3.GetObjectInput{ Bucket: aws.String(bucketName), Key: aws.String(*object.Key), }) if err != nil { panic("Couldn't get presigned URL for PutObject") } fmt.Printf("get URL For object: %s\n", presignResult.URL) } }

2023-05-21 · 1 分钟 · czyt

在树莓派3b上部署minio服务

安装 我的树莓派安装的是manjaro,直接执行如下命令即可 yay -S minio 官方的安装文档开源参考 https://min.io/docs/minio/linux/operations/install-deploy-manage/deploy-minio-single-node-single-drive.html 启用服务 systemctl enable minio 修改配置 主要修改mino的设置文件,文件位置为/etc/minio/minio.conf # Local export path. MINIO_VOLUMES="/srv/minio/data/" # Server user. MINIO_ROOT_USER=gopher # Server password. MINIO_ROOT_PASSWORD=gopher # Use if you want to run Minio on a custom port. MINIO_OPTS="--console-address :8888" MINIO_SERVER_URL="https://minio.xxx.org" MINIO_BROWSER_REDIRECT_URL="https://minio-console.xxx.org" 修改 MINIO_OPTS 主要是为了自定义Console的端口,而这个参数主要是在service定义中使用,安装软件后自动使用的service(路径为/usr/lib/systemd/system/minio.service)定义如下 [Unit] Description=Minio Documentation=https://docs.minio.io Wants=network-online.target After=network-online.target AssertFileIsExecutable=/usr/bin/minio [Service] # Specifies the maximum file descriptor number that can be opened by this process LimitNOFILE=65536 Type=simple User=minio Group=minio EnvironmentFile=/etc/minio/minio.conf ExecStartPre=/bin/bash -c "{ [ -z \"${MINIO_VOLUMES}\" ] && echo \"Variable MINIO_VOLUMES not set in /etc/minio/minio.conf\" && ex> ExecStart=/usr/bin/minio server $MINIO_OPTS $MINIO_VOLUMES Restart=always 启动服务 systemctl start minio ...

2023-04-12 · 1 分钟 · czyt

【译】如何使用 http.ResponseController 类型

本文原文地址 https://www.alexedwards.net/blog/how-to-use-the-http-responsecontroller-type ,使用chatGPT翻译 One of my favorite things about the recent Go 1.20 release is the new http.ResponseController type, which brings with it three nice benefits: 关于最近的 Go 1.20 版本,我最喜欢的事情之一是新的 http.ResponseController 类型,它带来了三个好处: You can now override your server-wide read and write deadlines on a per request basis. 您现在可以根据每个请求覆盖服务器范围内的读取和写入截止日期。 The pattern for using the http.Flusher and http.Hijacker interfaces is clearer and feels less hacky. No more type assertions necessary! 使用 http.Flusher 和 http.Hijacker 接口的模式更清晰,感觉更轻松。不再需要类型断言! It makes it easier and safer to create and use custom http.ResponseWriter implementations. 它使创建和使用自定义 http.ResponseWriter 实现变得更容易和更安全。 The first two benefits are mentioned in the release notes, but the third one seems to have gone under the radar a bit… which is a shame, because it’s very helpful! 发行说明中提到了前两个好处,但第三个好处似乎有点不为人知……这很遗憾,因为它非常有帮助! ...

2023-03-09 · 6 分钟 · czyt

golang CGO参考

开源项目 https://github.com/dolthub/go-library-sample https://github.com/draffensperger/go-interlang https://github.com/kbehouse/go_call_cxx_so https://github.com/tailscale/libtailscale https://github.com/iikira/golang-msvc https://github.com/vladimirvivien/go-cshared-examples https://github.com/tailscale/libtailscale/tree/main 文档 Embedding Go in C Calling C code from go C? Go? Cgo! CGO编程(Go语言高级编程) https://stackoverflow.com/questions/14581063/golang-cgo-converting-union-field-to-go-type https://sunzenshen.github.io/tutorials/2015/05/09/cgotchas-intro.html https://totallygamerjet.hashnode.dev/the-smallest-go-binary-5kb 代码片段 Convert ‘C’ array to golang slice func carray2slice(array *C.int, len int) []C.int { var list []C.int sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&list))) sliceHeader.Cap = len sliceHeader.Len = len sliceHeader.Data = uintptr(unsafe.Pointer(array)) return list }

2023-02-03 · 1 分钟 · czyt

Golang Web框架Buffalo 简单使用

官方文档 https://gobuffalo.io 安装 安装要求 Before installing make sure you have the required dependencies installed: A working Go environment Go version v1.16.0. Frontend Requirements# The following requirements are optional. You don’t need them if you want to build an API or if you prefer to build your app in an old-fashioned way. node version 8 or greater either yarn or npm for the asset pipeline built upon webpack . Database Specific Requirements# Again, if you don’t need a database, you won’t need these. ...

2023-01-29 · 1 分钟 · czyt

从Golang的开源项目中学习不同的功能实现

缘起 最近看到有些go开源项目中的代码,看到其中的功能,故整理备用。 密码 阈值解密(Threshold Decryption) 来源 取自公众号文章 package main import ( "crypto/rand" "fmt" "github.com/hashicorp/vault/shamir" ) func main() { // 1. 生成一个随机密钥(如AES-256密钥) secretKey := make([]byte, 32) _, err := rand.Read(secretKey) if err != nil { panic(err) } fmt.Printf("原始密钥: %x\n", secretKey) // 2. 分片为3份,至少需要2份恢复(阈值k=2,总分片n=3) shards, err := shamir.Split(secretKey, 3, 2) if err != nil { panic(err) } // 3. 模拟分片存储(实际中分发给不同机构) shard1, shard2, shard3 := shards[0], shards[1], shards[2] fmt.Printf("分片1: %x\n分片2: %x\n分片3: %x\n", shard1, shard2, shard3) // 4. 恢复密钥(任意2个分片) recoveredKey, err := shamir.Combine([][]byte{shard1, shard2}) // 使用分片1和2 if err != nil { panic(err) } fmt.Printf("恢复的密钥: %x\n", recoveredKey) } 数据结构 优先级队列 项目 https://github.com/tigrisdata/tigris ...

2022-11-25 · 62 分钟 · czyt

使用protoc-gen-star编写protoc插件

预备知识 需要安装的软件 protoc golang go 软件包 github.com/lyft/protoc-gen-star 插件调用步骤 protoc,PB编译器,使用一组标志(记录在protoc -h下)进行配置,并将一组文件作为参数交给它。在这种情况下,I标志可以被多次指定,是它在proto文件中用于导入依赖关系的查找路径。默认情况下,官方描述符protos已经被包含在内。 myplugin_out 告诉 protoc 使用 protoc-gen-myplugin protoc-plugin。这些插件会从系统的 PATH 环境变量中自动解析,或者可以用另一个标志明确指定。官方的protoc-plugins (例如,protoc-gen-python) 已经在protoc注册了。该标志的值是特定于特定插件的,但 :…/generated 后缀除外。这个后缀表示protoc将把该包生成的文件放在哪个根目录下(相对于当前工作目录)。然而,这个生成的输出目录不会传播给 protoc-gen-myplugin,所以它需要在标志的左边重复。PG* 通过一个 output_path 参数支持这一点。 protoc 解析传入的 proto 文件,确保它们在语法上是正确的,并加载任何导入的依赖项。它将这些文件和依赖关系转换成描述符 (它们本身就是 PB 消息),并创建一个 CodeGeneratorRequest (又是一个 PB)。protoc 将这个请求序列化,然后执行每个配置的 protoc-plugin,通过 stdin 发送有效载荷。 protoc-gen-myplugin 启动,接收请求的有效载荷,并将其解密。一个基于 PG* 的 protoc-plugin 有两个阶段。首先,PG* 对从 protoc 收到的 CodeGeneratorRequest 进行解密,并为每个文件和其包含的所有实体创建一个完全连接的抽象语法树 (AST)。为这个插件指定的任何参数也会被解析,以便以后使用。 当这一步完成后,PG*就会执行任何注册的模块,把构建的AST交给它。模块可以被写成生成人工制品(例如,文件),或者只是对所提供的图进行某种形式的验证而没有任何其他副作用。模块在针对PB的操作方面提供了极大的灵活性。 一旦所有的模块都被运行,PG*会将任何自定义的工件写入文件系统,或者将生成器特定的工件序列化为CodeGeneratorResponse并将数据发送到其stdout。这整个流程看起来像这样。 foo.proto → protoc → CodeGeneratorRequest → protoc-gen-myplugin → CodeGeneratorResponse → protoc → foo.pb.go 假设插件名称为diy,则需要编译程序为protoc-gen-diy,并将程序加入系统Path变量,通过下面的命令调用插件。 protoc -I . --diy_out=./gen/ xxxx.proto 使用protoc-gen-star包 模块 Modules PG*模块将被交付一个完整的AST,用于生成目标文件以及所有依赖项。然后,模块可以将文件添加到协议CodeGeneratorResponse或将文件作为组件直接写入磁盘。 ...

2022-10-29 · 3 分钟 · czyt

golang http Reverse Proxy使用备忘

创建 一般使用 使用 httputil.NewSingleHostReverseProxy 即可 返回Response 当我们想实现获取通过ReverseProxy的请求结果时,可以使用自定义的 responsewriter 来实现。参考定义 func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { .... } type ResponseWriter interface { // Header returns the header map that will be sent by // WriteHeader. The Header map also is the mechanism with which // Handlers can set HTTP trailers. // // Changing the header map after a call to WriteHeader (or // Write) has no effect unless the modified headers are // trailers. // // There are two ways to set Trailers. The preferred way is to // predeclare in the headers which trailers you will later // send by setting the "Trailer" header to the names of the // trailer keys which will come later. In this case, those // keys of the Header map are treated as if they were // trailers. See the example. The second way, for trailer // keys not known to the Handler until after the first Write, // is to prefix the Header map keys with the TrailerPrefix // constant value. See TrailerPrefix. // // To suppress automatic response headers (such as "Date"), set // their value to nil. Header() Header // Write writes the data to the connection as part of an HTTP reply. // // If WriteHeader has not yet been called, Write calls // WriteHeader(http.StatusOK) before writing the data. If the Header // does not contain a Content-Type line, Write adds a Content-Type set // to the result of passing the initial 512 bytes of written data to // DetectContentType. Additionally, if the total size of all written // data is under a few KB and there are no Flush calls, the // Content-Length header is added automatically. // // Depending on the HTTP protocol version and the client, calling // Write or WriteHeader may prevent future reads on the // Request.Body. For HTTP/1.x requests, handlers should read any // needed request body data before writing the response. Once the // headers have been flushed (due to either an explicit Flusher.Flush // call or writing enough data to trigger a flush), the request body // may be unavailable. For HTTP/2 requests, the Go HTTP server permits // handlers to continue to read the request body while concurrently // writing the response. However, such behavior may not be supported // by all HTTP/2 clients. Handlers should read before writing if // possible to maximize compatibility. Write([]byte) (int, error) // WriteHeader sends an HTTP response header with the provided // status code. // // If WriteHeader is not called explicitly, the first call to Write // will trigger an implicit WriteHeader(http.StatusOK). // Thus explicit calls to WriteHeader are mainly used to // send error codes. // // The provided code must be a valid HTTP 1xx-5xx status code. // Only one header may be written. Go does not currently // support sending user-defined 1xx informational headers, // with the exception of 100-continue response header that the // Server sends automatically when the Request.Body is read. WriteHeader(statusCode int) } 例子 package main import ( "fmt" "log" "net/http" "net/http/httputil" "net/url" "github.com/gin-gonic/gin" ) type customWriter struct { http.ResponseWriter } func NewCustomWriter(w http.ResponseWriter) *customWriter { return &customWriter{w} } func (c *customWriter) Header() http.Header { return c.ResponseWriter.Header() } func (c *customWriter) Write(data []byte) (int, error) { fmt.Println(string(data)) //get response here return c.ResponseWriter.Write(data) } func (c *customWriter) WriteHeader(i int) { c.ResponseWriter.WriteHeader(i) } func main() { router := gin.Default() router.GET("/", func(ctx *gin.Context) { targetURL, err := url.Parse("http://192.168.0.70:8090") if err != nil { log.Println(err) } proxy := httputil.NewSingleHostReverseProxy(targetURL) proxy.ServeHTTP(NewCustomWriter(ctx.Writer), ctx.Request) }) router.Run(":8899") } 封装个函数 ...

2022-09-30 · 4 分钟 · czyt

tailscale的泛型SingleFlight

源地址 tailscale仓库 完整代码如下 // Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package singleflight provides a duplicate function call suppression // mechanism. // // This is a Tailscale fork of Go's singleflight package which has had several // homes in the past: // // - https://github.com/golang/go/commit/61d3b2db6292581fc07a3767ec23ec94ad6100d1 // - https://github.com/golang/groupcache/tree/master/singleflight // - https://pkg.go.dev/golang.org/x/sync/singleflight // // This fork adds generics. package singleflight // import "tailscale.com/util/singleflight" import ( "bytes" "errors" "fmt" "runtime" "runtime/debug" "sync" ) // errGoexit indicates the runtime.Goexit was called in // the user given function. var errGoexit = errors.New("runtime.Goexit was called") // A panicError is an arbitrary value recovered from a panic // with the stack trace during the execution of given function. type panicError struct { value interface{} stack []byte } // Error implements error interface. func (p *panicError) Error() string { return fmt.Sprintf("%v\n\n%s", p.value, p.stack) } func newPanicError(v interface{}) error { stack := debug.Stack() // The first line of the stack trace is of the form "goroutine N [status]:" // but by the time the panic reaches Do the goroutine may no longer exist // and its status will have changed. Trim out the misleading line. if line := bytes.IndexByte(stack[:], '\n'); line >= 0 { stack = stack[line+1:] } return &panicError{value: v, stack: stack} } // call is an in-flight or completed singleflight.Do call type call[V any] struct { wg sync.WaitGroup // These fields are written once before the WaitGroup is done // and are only read after the WaitGroup is done. val V err error // forgotten indicates whether Forget was called with this call's key // while the call was still in flight. forgotten bool // These fields are read and written with the singleflight // mutex held before the WaitGroup is done, and are read but // not written after the WaitGroup is done. dups int chans []chan<- Result[V] } // Group represents a class of work and forms a namespace in // which units of work can be executed with duplicate suppression. type Group[K comparable, V any] struct { mu sync.Mutex // protects m m map[K]*call[V] // lazily initialized } // Result holds the results of Do, so they can be passed // on a channel. type Result[V any] struct { Val V Err error Shared bool } // Do executes and returns the results of the given function, making // sure that only one execution is in-flight for a given key at a // time. If a duplicate comes in, the duplicate caller waits for the // original to complete and receives the same results. // The return value shared indicates whether v was given to multiple callers. func (g *Group[K, V]) Do(key K, fn func() (V, error)) (v V, err error, shared bool) { g.mu.Lock() if g.m == nil { g.m = make(map[K]*call[V]) } if c, ok := g.m[key]; ok { c.dups++ g.mu.Unlock() c.wg.Wait() if e, ok := c.err.(*panicError); ok { panic(e) } else if c.err == errGoexit { runtime.Goexit() } return c.val, c.err, true } c := new(call[V]) c.wg.Add(1) g.m[key] = c g.mu.Unlock() g.doCall(c, key, fn) return c.val, c.err, c.dups > 0 } // DoChan is like Do but returns a channel that will receive the // results when they are ready. // // The returned channel will not be closed. func (g *Group[K, V]) DoChan(key K, fn func() (V, error)) <-chan Result[V] { ch := make(chan Result[V], 1) g.mu.Lock() if g.m == nil { g.m = make(map[K]*call[V]) } if c, ok := g.m[key]; ok { c.dups++ c.chans = append(c.chans, ch) g.mu.Unlock() return ch } c := &call[V]{chans: []chan<- Result[V]{ch}} c.wg.Add(1) g.m[key] = c g.mu.Unlock() go g.doCall(c, key, fn) return ch } // doCall handles the single call for a key. func (g *Group[K, V]) doCall(c *call[V], key K, fn func() (V, error)) { normalReturn := false recovered := false // use double-defer to distinguish panic from runtime.Goexit, // more details see https://golang.org/cl/134395 defer func() { // the given function invoked runtime.Goexit if !normalReturn && !recovered { c.err = errGoexit } c.wg.Done() g.mu.Lock() defer g.mu.Unlock() if !c.forgotten { delete(g.m, key) } if e, ok := c.err.(*panicError); ok { // In order to prevent the waiting channels from being blocked forever, // needs to ensure that this panic cannot be recovered. if len(c.chans) > 0 { go panic(e) select {} // Keep this goroutine around so that it will appear in the crash dump. } else { panic(e) } } else if c.err == errGoexit { // Already in the process of goexit, no need to call again } else { // Normal return for _, ch := range c.chans { ch <- Result[V]{c.val, c.err, c.dups > 0} } } }() func() { defer func() { if !normalReturn { // Ideally, we would wait to take a stack trace until we've determined // whether this is a panic or a runtime.Goexit. // // Unfortunately, the only way we can distinguish the two is to see // whether the recover stopped the goroutine from terminating, and by // the time we know that, the part of the stack trace relevant to the // panic has been discarded. if r := recover(); r != nil { c.err = newPanicError(r) } } }() c.val, c.err = fn() normalReturn = true }() if !normalReturn { recovered = true } } // Forget tells the singleflight to forget about a key. Future calls // to Do for this key will call the function rather than waiting for // an earlier call to complete. func (g *Group[K, V]) Forget(key K) { g.mu.Lock() if c, ok := g.m[key]; ok { c.forgotten = true } delete(g.m, key) g.mu.Unlock() }

2022-09-23 · 5 分钟 · czyt