In the physical world, Iran’s authoritarian leaders answer to no one. They try, but often fail, to keep Iranians away from Western entertainment and news. Thanks to their rules, women are required to shroud their hair with head scarves, their bodies with loose clothing.

On the internet, Iranians are often able to slip those bonds.

They squeal over the Korean boy band BTS and the actor Timothée Chalamet. They post Instagram selfies: no head scarf, just hair. They can watch leaked videos of appalling conditions in Iranian prisons, inspect viral photos of the luxurious lives that senior officials’ children are leading abroad while the economy collapses at home, read about human rights abuses, swarm politicians with questions on Twitter and jeer their supreme leader, anonymously, in comments.

“In one world, the government controlled everything, and people always had to hide what they think, what they want, what they like, what they enjoy in their real life,” said Mohammad Mosaed, an Iranian investigative journalist who has been arrested twice for posting content online that the government considered objectionable.

“But on the internet, people had a chance to say what they want, to show who they really are,” he said. “And that caused conflict between the two worlds.”

Among Iranians, growing online outrage has helped fuel successive waves of protest against the autocratic clerics who rule them, culminating this month in countrywide demonstrations that have challenged the foundations of the Islamic Republic.

Though the battle is being fought with bodies in the street, with women burning their head scarves and Iranians of all classes confronting security forces, it was protesters’ phones that first swept them there.

News broke online on Sept. 16 that a young woman had died in police custody after being accused of violating Iran’s mandatory head scarf law. Within a day, a quarter-million Instagram users had joined a digital chain of Iranians posting about the woman, Mahsa Amini, and the hashtag bearing her name had been tweeted, retweeted or liked more than nine million times.

Dozens of cities have erupted in protest every night since. Security forces have killed at least 50 people, according to rights groups and arrested more than 700 people, including journalists and activists who were using social media to keep people informed.

Dozens of prominent athletes, including the national soccer teams stars Ali Karimi and Sardar Azmoun, celebrities and notable directors such as Asghar Farhadi have used social media to announce their support for the protesters in the past week. The government has said they will face repercussions, including a ban on professional activity.

The government has responded to the unrest with more than bullets, tear gas and beatings.

Nightly internet and app outages confound efforts to organize new protests and slow their momentum. But far beyond these protests, Iran’s leaders have worked for more than a decade to shore up control by building their own domestic internet, complete with copycat versions of Google and Instagram. That has put their goal of shutting out the rest of the wider internet nearly within reach.

Under Ebrahim Raisi, Iran’s new, ultraconservative president, Iran has intensified censorship, disrupting V.P.N.s, impeding the encryption on messaging apps and restricting Google searches to Safe Search, which shows only age-appropriate content for children under 13.

There are fears that a pending internet bill will block the remaining social media apps, on which an estimated 11 million Iranians rely for their livelihoods, working as influencers, selling products through Instagram and more.

Iran’s enemies are using social media in an “onslaught to distort and destroy” the clerical establishment, the country’s 83-year-old supreme leader, Ayatollah Ali Khamenei, warned in a February speech, calling on the authorities to regulate internet access.

“The judiciary must prevent the minds of the people from becoming worried and disturbed” by rumors and false claims on “both the media and the internet,” he said in June.

Nearly 80 percent of Iranians use some form of social media, according to a survey this year by a government-affiliated group. Even many government officials are on Twitter, even though it is banned in Iran, in a tacit acknowledgment of its reach.

Recognizing that the internet blackouts could smother the protests, the Biden administration changed regulations last week to give American tech companies more room to offer services to Iranians without running afoul of United States sanctions on Iran. But it is unclear how quickly they could act.

In a country where media outlets are tightly controlled and leaders almost never have to submit to public questioning, platforms like Twitter, Instagram and Clubhouse constitute the only means of holding the powers that be to account.

“It’s been critical for a lot of people to wake up and see what’s really going on,” said Shahin Milani, the executive director of the U.S.-based Iran Human Rights Documentation Center. “And that’s really critical because no other outlet provides that.”

Online revelations about abuses and double standards have stoked indignation and disgust among Iranians in recent years.

Some touched on the brutal crackdowns on antigovernment protests and the law that requires women to cover their bodies and hair. This summer, before Ms. Amini’s story broke, several videos circulated on social media in which Iran’s notorious morality police violently detained young women they deemed improperly covered.

But even ostensibly trivial content could draw rage.

There were the photos in April showing that the family of the country’s Parliament speaker had gone abroad to buy baby clothes at a time when most Iranians could barely afford cheap Iranian-made onesies.

And in 2017, a viral video of the son of a prominent lawmaker crediting his career success to “great genes” touched a raw nerve among those with fewer connections and more troubles.

That fall, Iranians flocked to a social media campaign with the hashtag “I regret,” uniting people who rued voting for reformist candidates who failed to enact change.

By the end of 2017, protests triggered by investments gone bad had set off nationwide protests against the government and its economic policies.

The authorities have seen the unfettered internet as a threat since 2009, when social media helped mobilize millions of Iranians in the Green Movement protests over what they believed was a rigged presidential election.

Once focused on developing a chaste domestic version of the internet, the government turned its energies to creating one it could control.

“If I didn’t have access to the internet, I’d believe everything they wanted to tell me,” said Amir Rashidi, the director of digital rights and security at Miaan Group, which is based in the United States and supports human rights groups in Iran. “So they realized, that’s where they’re being hit and they need to control it.”

Under former President Hassan Rouhani, a relative moderate in office from 2013 until last year, restrictions that capped internet speed were lifted and mobile internet took off. Mr. Rouhani also talked of allowing Western tech companies such as Twitter into the country under China-style conditions that would require them to impose user restrictions.

But stringent U.S. sanctions on Iran over its nuclear program made Silicon Valley reluctant or unable to work with Iran.

Instead, Iran built its own versions of Google, Instagram, WhatsApp and more, making sure the content was to its liking.

Statistics from Iran’s own app stores, however, show that only a few million people in a country of about 85 million have downloaded them. Researchers say that is partly because of concerns about government surveillance.

And Iranians keep finding ways into the wider internet: About 80 percent of Iranians rely on virtual private networks and proxies for access, a lawmaker told state media in July.

“Iranians also see how the rest of the world lives and want that too,” said Holly Dagres, an Iranian-American senior fellow at the Atlantic Council who has written a report on Iranians’ use of social media. “But more importantly, it’s the only way for their voices to be heard.”

Now, when the broader internet goes dark, Iran’s National Information Network stays up, enticing Iranians to migrate. State television has taken to promoting the homegrown apps during the current protests, informing viewers that while foreign apps must be regulated to prevent the “rioters” from doing further damage, the public is free to use their Iranian versions.

One solution, Iranian activists say, is for American tech companies to re-enter the field in Iran after backing away when President Donald J. Trump imposed tougher American sanctions on Iran.

Signal, a secure messaging app, said it and volunteer users were working to devise alternative ways of accessing and distributing Signal, but it had encountered hurdles, including Iranian telecommunications companies preventing validation codes from being delivered over text. Google said it was working on technical adjustments to help with access. But more sweeping solutions did not appear forthcoming.

“The main tool that we have to combat” Iranian government controls, Mr. Rashidi said, “is breaking the isolation.”

Farnaz Fassihi contributed reporting.