DevOps Lesson
import socket
# Change the following host and see what IP it prints!
host = "discord.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
-
What is an IP address?
Internet Protocol address is a numerical label that is connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions: network interface identification and location addressing.
-
What is a TCP port?
TCP is called as Transmission control protocol which provides a communication service between an application program and the Internet protocol (IP) A port is a number used to uniquely identify a transaction over a network by specifying both the host and the service.(saftey mechanism to send groups of bytes over the internet.)
Slide Hacks
-
DNS stands for Domain Name System.
-
The purpose of DNS is to translate human-readable domain names, such as google.com, into IP addresses that computers can understand. DNS allows users to access websites and other network resources by using memorable domain names instead of having to remember the IP addresses of every resource they want to access.
-
When a user types a domain name into their browser, the browser sends a request to a DNS resolver to obtain the IP address associated with that domain name. The resolver then queries the DNS hierarchy, starting with the root servers, to obtain the IP address for the requested domain name. The IP address is then returned to the resolver, which in turn returns it to the user's browser. The browser can then use the IP address to establish a connection to the desired resource.
-
A DNS resolver is a component of the DNS system that is responsible for resolving domain names into IP addresses. Resolvers can be configured on individual computers or on network servers. When a resolver receives a request for a domain name, it queries the DNS hierarchy to obtain the IP address associated with that name. Resolvers cache the results of previous queries to improve performance and reduce network traffic.
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://google.com")
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 + "/products")
print(response.headers)
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
- Research 1 HTTP header and describe, in detail, its purpose.
One example of an HTTP header is the "Content-Type" header. The Content-Type header is used to indicate the media type of the resource that is being sent in the HTTP response. For example, if you were sending an HTML file as the response to an HTTP request, you would include the "Content-Type: text/html" header in the response. This tells the browser that the resource being sent is an HTML document.
- 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
}
- 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 to improve the availability, scalability, and performance of web applications by ensuring that no single server becomes overloaded with traffic. NGINX uses different load balancing algorithms to determine how to distribute traffic, such as round-robin, least connections, and IP hash.
- 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
-
CORS stands for Cross-Origin Resource Sharing. It is a security feature implemented in web browsers to restrict cross-origin HTTP requests that are initiated by web scripts.
-
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.
-
CORS is important for security reasons because it prevents unauthorized cross-site scripting attacks. By implementing CORS, you can restrict which domains can access your resources, which prevents attackers from accessing sensitive data or executing malicious scripts on your website. Additionally, CORS allows you to share resources between domains, which can be useful for web applications that require data from multiple sources.
-
By understanding how to use CORS, you can make your websites more secure and prevent potential attacks. Additionally, if you are building web applications that require data from multiple sources, CORS can help you share resources between domains and improve the user experience. Finally, as web technology continues to evolve, it is likely that CORS will become an increasingly important security feature, so understanding how to use it will be beneficial for your future career as a web developer.
Total: 0.2 points
KASM Hacks
-
"sudo" is a command that allows you to run other commands as an administrator in terminal. It gives you permission to do things like installing software or making changes to the system.
-
Some commands to look at how a machine's storage is set up include "df -h" to see how much space is left on file systems, "lsblk" to see what storage devices are available, and "mount" to see what file systems are currently being used.
-
Alternatives to "curl -O" for downloading the KASM zip file include using a web browser, a download manager app, or a different command-line tool like "wget".
-
"install.sh" is a command that installs and configures the KASM software on your computer. It may include downloading necessary files and setting up configuration options.
-
Deploying KASM requires knowledge of using the command line, managing software packages, and securing networks. You can expand this guide by discussing other topics like setting up firewalls, user permissions, and integrating KASM with other security tools.
Total: 0.2 points