Skip to content

Commit

Permalink
Set nhp-token cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-0824 committed Oct 22, 2024
1 parent 0eff4f4 commit 7498f40
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type HttpConfig struct {
HttpListenIp string
TLSCertFile string
TLSKeyFile string
AccessControlAllowOrigin string
}

type Peers struct {
Expand Down
8 changes: 5 additions & 3 deletions server/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type HttpServer struct {
ginEngine *gin.Engine
listenAddr *net.TCPAddr

accessControlAllowOrigin string

wg sync.WaitGroup
running atomic.Bool

Expand Down Expand Up @@ -67,7 +69,7 @@ func (hs *HttpServer) Start(us *UdpServer, hc *HttpConfig) error {

gin.SetMode(gin.ReleaseMode)
hs.ginEngine = gin.New()
hs.ginEngine.Use(corsMiddleware())
hs.ginEngine.Use(corsMiddleware(hc.AccessControlAllowOrigin))
hs.ginEngine.Use(gin.LoggerWithWriter(us.log.Writer()))
hs.ginEngine.Use(gin.Recovery())

Expand Down Expand Up @@ -282,10 +284,10 @@ func (hs *HttpServer) initRouter() {
// corsMiddleware is a middleware function that adds CORS headers to the HTTP response.
// It allows cross-origin resource sharing, specifies allowed methods, exposes headers, and sets maximum age.
// If the request method is OPTIONS, PUT, or DELETE, it aborts the request with a 204 status code.
func corsMiddleware() gin.HandlerFunc {
func corsMiddleware(originResource string) gin.HandlerFunc {
return func(c *gin.Context) {
// HTTP headers for CORS
c.Writer.Header().Set("Access-Control-Allow-Origin", "*") // allow cross-origin resource sharing
c.Writer.Header().Set("Access-Control-Allow-Origin", originResource) // allow cross-origin resource sharing
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST") // methods
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Type, Content-Length, Set-Cookie")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, X-NHP-Ver, Cookie")
Expand Down
2 changes: 2 additions & 0 deletions server/main/etc/http.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# EnableTLS: whether to use TLS certificates for hosting https server.
# TLSCertFile: certificate file path.
# TLSKeyFile: key file path.
# AccessControlAllowOrigin: the response header indicates whether the response can be shared with requesting code from the given origin.
# to update http changes, you need to restart the http server by changing "EnableHttp" to "false" and then switch it back to "true".
EnableHttp = true
EnableTLS = true
HttpListenIp = "0.0.0.0" # empty for ipv4 + ipv6, "0.0.0.0" for ipv4 only, "127.0.0.1" for local ipv4 access only
TLSCertFile = "cert/cert.pem"
TLSKeyFile = "cert/cert.key"
AccessControlAllowOrigin = "https://demologin.opennhp.cn"
4 changes: 3 additions & 1 deletion server/plugins/example/templates/example_login.html
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ <h2 id="authSuccessMessage"></h2>
"&password=" + encodeURIComponent(password);
console.log(nhpValidUrl);

fetch(nhpValidUrl)
fetch(nhpValidUrl,{
credentials: "include"
})
.then(response => response.json())
.then(result => {
console.log(result);
Expand Down

0 comments on commit 7498f40

Please sign in to comment.