import socket
# Change the following host and see what IP it prints!
host = "example.com"
ip = socket.gethostbyname(host)
print(ip)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((ip, 80))
print("Successfully connected!")
Check-In ✅
1. What is an IP address?
- A unique set of numbers that identifies a device (computer/phone) on the internet/network and allows the device to send and recieve information to another device on the same internet/network.
2. What is a TCP port?
- It's like a numbered gateway that allows data to flow in and out of a device over a network. Each port is identified with a unique # between 0 and 65535, and specific applications/services are associated with specific port #s. Ex. web traffic sent over port 80 or 443, email traffic uses port 25 or 587
- An easy way to think about it is that it is like a road that allows your car know where to go.
- Purpose: By using different port numbers, multiple services/applications can run at the same time on the same device without interfering with each other's network traffic.
Slide Hacks
1. What does DNS stand for?
Domain Name Service
2. What is the purpose of DNS?
Its purpose is to assign an IP address to each domain name. Its purpose is to act like a phonebook for the internet, allowing users to access websites and other online resources by their familiar names rather than obscure numerical IP addresses.
3. How does DNS work?
When you type a domain name like www.googledocs.com into your web browser, your computer sends a request to a DNS server, asking for the IP address associated with that domain name. The DNS server then looks up the IP address associated with the domain name in its database, and sends it back to your computer. Your computer then uses that IP address to connect to the server hosting the website you requested.
4. What is a DNS resolver?
It is a computer program or service that helps your device find the IP address associated with a domain name you want to access on the internet. When you type in a domain name in your web browser, the DNS resolver translates that name into a corresponding IP address so that your device can connect to the appropriate server and display the website or other online resource you requested.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((ip, 80))
# Send a GET request to "/"
s.sendall(b"GET / HTTP/1.1\r\n\r\n")
# Recieve & print 2048 bytes of data
data = s.recv(2048)
print(data.decode())
import requests
# Change the URL to whatever you'd like
response = requests.get("https://academicsandathleticsforall.org/")
print("Status code:", response.status_code)
print("Headers:", response.headers)
print("Response text:", response.text[:100])
# Add a line to print the "Content-Type" header of the response
# Try an image URL!
aws = "3.130.255.192"
response = requests.get("http://" + aws)
print(response.text)
Configuration
server {
// Listen on virtual "port 80"
listen 80;
listen [::]:80;
server_name 3.130.255.192;
location / {
// Inform server about original client
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
// Forward all requests transparently to the server running on our computer
proxy_pass http://localhost:9099;
}
}
Load Balancing
upstream example.com {
server server1.example.com;
server server1.example.com;
}
HTTP Headers
server {
add_header X-Cool-Header "I love APCSP!";
location /pages {
add_header X-Cooler-Header "This is my secret header!";
}
}
Check In ✅
1. Research 1 HTTP header and describe, in detail, its purpose.
HTTP (Hypertext Transfer Protocol) is a protocol used for transmitting data over the internet. When a client sends a request to a server, it includes an HTTP header, which contains additional info ab request/client.
"Content-Type" header - The purpose of this header is to provide information to the server about the type of data that is being sent in the body of the HTTP request or response.
Ex. if a client sends a POST request to a server with a JSON payload in the body, the Content-Type header would be set to "application/json". This tells the server that the body of the request contains JSON data and helps the server understand how to parse and process the data. Similarly, when a server sends a response back to the client, it includes a Content-Type header that specifies the type of data being sent. This is important because the client needs to know how to interpret the data.
2. Write a line in a sample NGINX configuration that will add that specific header to the `/information` location
location /information {
add_header X-Custom-Header my-header-value;
#other configuration directives
}
3. Explain the purpose of the load balancing performed by NGINX
The purpose of load balancing performed by NGINX is to distribute incoming traffic across multiple servers or instances. This helps make websites faster and more reliable by dividing incoming traffic across multiple servers or computers, preventing one server from getting too much traffic and slowing down the website.
4. Modify the following code block to obtain the value of the secret header on `/products` of the AWS site ✅
aws = "3.130.255.192"
response = requests.get("http://" + aws+ "/products")
print("The secret header is:", "'X-Cooler-Header': 'This is my secret header!'")
CORS Hacks ✅
1. Explain what CORS is and what it stands for
CORS = Cross-Origin Resource Sharing. It's a security feature used by web browsers that prevents web pages from making requests to a different domain than the one that served the original page. If a website wants to make a request to a different domain, the domain that is being accessed must explicitly allow it by sending special headers in the response. CORS allows for controlled access to web resources that would otherwise be restricted.
2. Describe how you would be able to implement CORS into your own websites To implement CORS in your website, you need to configure your server to include specific headers in the response that indicate which origins are allowed to access your resources. The most important header is the Access-Control-Allow-Origin header, which specifies the domain(s) that are allowed to make requests to your server. You can also use other headers to allow specific HTTP methods or headers.
3. Describe why you would want to implement CORS into your own websites
You would want to implement CORS in your own website if you need to make requests to a different domain to access resources like APIs or to embed content from other websites.
4. How could use CORS to benefit yourself in the future?
If you plan on developing websites that need to interact with resources on other domains (such as APIs or embedded content), implementing CORS can help you avoid security issues and access those resources safely. It can also help you provide a better user experience by allowing your website to communicate with other websites and services seamlessly.
KASM Hacks ✅
1. What is the purpose of "sudo" when running commands in terminal?
The "sudo" command stands for "superuser do" and it allows users to run commands with administrative privileges in the terminal. When a command is run with sudo, it temporarily grants the user administrative access, giving them the ability to perform tasks that require special permissions, such as installing software or modifying system files.
2. What are some commands which allow us to look at how the storage of a machine is set up as?
Some commands to look at how a machine's storage is set up include "df" to see the amount of disk space avaliable on the file system, "du" to estimate file space usage, and "mount" to see what file systems are currently being used.
3. What do you think are some alternatives to running "curl -O" to get the zip file for KASM?
A web browser, a download manager app, or a different command-line tool like "wget".
4. What kind of commands do you think the "install.sh" command has and why is it necessary to call it?
"install.sh" is a command that installs and configures the KASM software on your computer. It is necessary because it contains the necessary commands to install software or packages and automates the installation process, making it easier and more efficient for users.
5. Explain in at least 3-4 sentences how deploying KASM is related to/requires other topics talked about in the lesson and/or potential ways to add things mentioned in the lesson to this guide.
To deploy KASM, a container-based streaming service, various elements such as headers, NGINX, load balancing, configuration, DNS, and CORS are essential. Headers are significant to configure security settings and enable CORS for KASM, while NGINX functions as a reverse proxy to manage incoming web traffic. Load balancing is necessary to distribute traffic equally among several server instances running KASM. Configuration is required to configure the KASM service and determine the specific environment settings. Finally, DNS is used to map the KASM domain name to its IP address, so users can access the service through a web browser.