Giter VIP home page Giter VIP logo

reverse-proxy's Introduction

Reverse Proxy with Middleware in Go

This Go program demonstrates a simple reverse proxy server with middleware using the net/http package. The proxy server intercepts incoming HTTP requests, modifies them, and forwards them to a target URL. The implementation includes middleware for IP address filtering and rate limiting.

Overview

  • The program defines an IP middleware that restricts access based on a predefined set of allowed IP addresses.
  • It includes a rate limiter middleware that enforces a maximum number of requests per minute.
  • The handleRequest function acts as the main handler for incoming requests. It modifies the response body and forwards the request to a target URL.
  • The server runs on port 80 and responds to requests on the root ("/") and "/qa.example.com/" paths.

Code Description

Middleware Functions

IPMiddleware

// IPMiddleware is a middleware that restricts access based on IP address
func IPMiddleware(allowedIPs map[string]bool) func(http.Handler) http.Handler {
    return func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            // Check if the request's IP is in the allowedIPs set
            clientIP := strings.Split(r.RemoteAddr, ":")[0]
            if !allowedIPs[clientIP] {
                http.Error(w, "Forbidden", http.StatusForbidden)
                fmt.Printf("Access denied for IP: %s\n", clientIP)
                return
            }

            // Call the next handler if the IP is allowed
            next.ServeHTTP(w, r)
        })
    }
}

reverse-proxy's People

Contributors

nitin-gupta-xecurify avatar

Watchers

Nitin Gupta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.