code.gitea.io/gitea@v1.21.7/routers/private/internal.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  // Package private contains all internal routes. The package name "internal" isn't usable because Golang reserves it for disabling cross-package usage.
     5  package private
     6  
     7  import (
     8  	"net/http"
     9  	"strings"
    10  
    11  	"code.gitea.io/gitea/modules/context"
    12  	"code.gitea.io/gitea/modules/log"
    13  	"code.gitea.io/gitea/modules/private"
    14  	"code.gitea.io/gitea/modules/setting"
    15  	"code.gitea.io/gitea/modules/web"
    16  
    17  	"gitea.com/go-chi/binding"
    18  	chi_middleware "github.com/go-chi/chi/v5/middleware"
    19  )
    20  
    21  // CheckInternalToken check internal token is set
    22  func CheckInternalToken(next http.Handler) http.Handler {
    23  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    24  		tokens := req.Header.Get("Authorization")
    25  		fields := strings.SplitN(tokens, " ", 2)
    26  		if setting.InternalToken == "" {
    27  			log.Warn(`The INTERNAL_TOKEN setting is missing from the configuration file: %q, internal API can't work.`, setting.CustomConf)
    28  			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
    29  			return
    30  		}
    31  		if len(fields) != 2 || fields[0] != "Bearer" || fields[1] != setting.InternalToken {
    32  			log.Debug("Forbidden attempt to access internal url: Authorization header: %s", tokens)
    33  			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
    34  		} else {
    35  			next.ServeHTTP(w, req)
    36  		}
    37  	})
    38  }
    39  
    40  // bind binding an obj to a handler
    41  func bind[T any](_ T) any {
    42  	return func(ctx *context.PrivateContext) {
    43  		theObj := new(T) // create a new form obj for every request but not use obj directly
    44  		binding.Bind(ctx.Req, theObj)
    45  		web.SetForm(ctx, theObj)
    46  	}
    47  }
    48  
    49  // Routes registers all internal APIs routes to web application.
    50  // These APIs will be invoked by internal commands for example `gitea serv` and etc.
    51  func Routes() *web.Route {
    52  	r := web.NewRoute()
    53  	r.Use(context.PrivateContexter())
    54  	r.Use(CheckInternalToken)
    55  	// Log the real ip address of the request from SSH is really helpful for diagnosing sometimes.
    56  	// Since internal API will be sent only from Gitea sub commands and it's under control (checked by InternalToken), we can trust the headers.
    57  	r.Use(chi_middleware.RealIP)
    58  
    59  	r.Post("/ssh/authorized_keys", AuthorizedPublicKeyByContent)
    60  	r.Post("/ssh/{id}/update/{repoid}", UpdatePublicKeyInRepo)
    61  	r.Post("/ssh/log", bind(private.SSHLogOption{}), SSHLog)
    62  	r.Post("/hook/pre-receive/{owner}/{repo}", RepoAssignment, bind(private.HookOptions{}), HookPreReceive)
    63  	r.Post("/hook/post-receive/{owner}/{repo}", context.OverrideContext, bind(private.HookOptions{}), HookPostReceive)
    64  	r.Post("/hook/proc-receive/{owner}/{repo}", context.OverrideContext, RepoAssignment, bind(private.HookOptions{}), HookProcReceive)
    65  	r.Post("/hook/set-default-branch/{owner}/{repo}/{branch}", RepoAssignment, SetDefaultBranch)
    66  	r.Get("/serv/none/{keyid}", ServNoCommand)
    67  	r.Get("/serv/command/{keyid}/{owner}/{repo}", ServCommand)
    68  	r.Post("/manager/shutdown", Shutdown)
    69  	r.Post("/manager/restart", Restart)
    70  	r.Post("/manager/reload-templates", ReloadTemplates)
    71  	r.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues)
    72  	r.Post("/manager/pause-logging", PauseLogging)
    73  	r.Post("/manager/resume-logging", ResumeLogging)
    74  	r.Post("/manager/release-and-reopen-logging", ReleaseReopenLogging)
    75  	r.Post("/manager/set-log-sql", SetLogSQL)
    76  	r.Post("/manager/add-logger", bind(private.LoggerOptions{}), AddLogger)
    77  	r.Post("/manager/remove-logger/{logger}/{writer}", RemoveLogger)
    78  	r.Get("/manager/processes", Processes)
    79  	r.Post("/mail/send", SendEmail)
    80  	r.Post("/restore_repo", RestoreRepo)
    81  	r.Post("/actions/generate_actions_runner_token", GenerateActionsRunnerToken)
    82  
    83  	return r
    84  }