Created
December 22, 2020 09:32
-
-
Save ficapy/20eb19edf08a3a4bbdb0d41b84670725 to your computer and use it in GitHub Desktop.
用nc接收浏览器请求的时候有跨域OPTION请求无法跳过,写了个替代的小脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| "github.com/gin-gonic/gin" | |
| "net/http/httputil" | |
| ) | |
| func CORS() gin.HandlerFunc { | |
| return func(c *gin.Context) { | |
| c.Writer.Header().Set("Access-Control-Allow-Origin", "*") | |
| c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") | |
| c.Writer.Header().Set("Access-Control-Allow-Headers", "*") | |
| c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH") | |
| if c.Request.Method == "OPTIONS" { | |
| c.AbortWithStatus(204) | |
| return | |
| } | |
| c.Next() | |
| } | |
| } | |
| func main() { | |
| router := gin.Default() | |
| router.Use(CORS()) | |
| router.Any("/*any", func(c *gin.Context) { | |
| fmt.Println(c.Request.Host, c.Request.RemoteAddr, c.Request.RequestURI) | |
| requestDump, err := httputil.DumpRequest(c.Request, true) | |
| if err != nil { | |
| fmt.Println(err) | |
| } | |
| fmt.Println(string(requestDump)) | |
| c.JSON(200, gin.H{"message": "pong"}) | |
| }) | |
| addr := flag.String("a", "127.0.0.1:8080", "listen addr") | |
| flag.Parse() | |
| router.Run(*addr) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment